How to remove a parameter from query string and rewrite URL? - windows

I have a URL that appears like this
http://www.domain.com/previous-winners/?ceremony=406&title=2015
and i'm trying to rewrite this with my .htaccess file to appear like this. Keeping the title parameter and dropping the ceremony one.
http://www.domain.com/previous-winners/2015
This is what i have so far
RewriteRule ^previous-winners/$2 /previous-winners/?ceremony=$1&title=$2 [NC]
But i'm not really sure where to go next.

Use this rule in your .htaccess:
RewriteEngine On
RewriteRule ^previous-winners/([^/]*)$ /previous-winners/?ceremony=406&title=$1 [L]
It will leave you with the URL: http://www.domain.com/previous-winners/2015
Just make sure you clear your cache before you test this.

Related

Rewrite second URL Parameter htaccess

I have searched in SO but never came across this specific answer. If anyone can help.
This use to be my URL structure /index.php?param1=is1 and I got it to look like this /is1 by using this Rewrite.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
Do not ask my how I got this as it's been a while that this was written. Now the issue is i have a second parameter. So my url that is like this /index.php?param1=is1&param2=is2 needs to be like this /is1/is2.
Obviously, now it's like this /is1&param2=is2. So i guess what I am searching for is a way to ask if &param2= is seen than hide it ?
I really don't get the logic behind all this URLREWRITE. I am trying to clean my URL structure for SEO purposes and COPY/PASTE logic as URL's should be shared between different users / clients. Obviously, having /this-is-something&this=that is not that bad but having /this-is-something/that is much better.
Just for info, first parameters is BRANDS /this-is-my-brand1 or /this-is-my-brand2 and second parameter is a product as /this-is-my-brand1/product1 before all that I use to have /index.php?brand=brand-one&product=product-one. Changing everything is not an issue, I can start from scratch if there is anything better than what I have.
I am on the learning curve, so I don't mind long explanations.
Any help is appreciated.
If your request parameters name are static (never change) you can use a more specifiq rule
RewriteRule ^(.+)/(.+)$ index.php?brand=$1&product=$2 [L]

301 redirect with RewriteRules

I have a problem concerning RewriteRules.
I'd like to move one page permanently, so I want to use a 301 redirect. I tried this:
RewriteRule ^page1/([A-Z].*)$ http://www.abs.nl/page1/vraag-$1 [R=301]
However this does not work. Can someone please tell me how I could fix this? I already tried for hours to find an answer.
Should I also use a %{HTTP_HOST} condition? I see this a lot but I don't know how it works.
A couple things:
Make sure that you have RewriteEngine On before your rewrite rule.
Your current regex is looking for a capital letter followed by any character, is this what you want?
Try this:
RewriteEngine On
RewriteRule ^page/(.*)$ http://www.abs.nl/page1/vraag-$1 [R=301,L]
That redirect will match on any string of characters following page/ in a URL.

Why are my .htaccess rules not working?

I have a cascade problem with my .htaccess rules. Consider the following:
RewriteEngine on
RewriteRule ^product/(.*)$ product.php [L,QSA]
RewriteRule ^(.*)$ index.php [L]
With the above, if I requested a URL like http://example.com/product/product-slug, then I’d expect the request to get routed to product.php. However, it doesn’t; my index.php script is picked the request up.
I would have thought that the first RewriteRule would be matched, and as it has a L (last) flag that no further RewriteRules would be matched, including the “catch-all” one at the bottom.
Why is this not working as expected?
This should sort it:
RewriteRule ^product/(.*)$ product.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !product.php
RewriteRule ^.*$ index.php [L]
The problem is that because the rules were in different sets, i.e. not attached a condition, it only stopped processing the current set of rules (the first one) and jumped onto the second.
Hope that clears it all up :)
Perhaps a typo in your code? You're writing "http://example.com/products/" in your question, but in the code you're targeting ^product$, with no s.
Also, your first rule is too strict. It will only match http://example.com/product/. You need to include a wild card after product to allow it to pick up product-slug. Something like RewriteRule ^products/(.*)$ product.php [L,QSA] should work.
Is it not because of the order you have placed the rules in? The one below will override changes to the one above it. Try changing them around.
Also, do you need to set the RewriteBase or not? Is your project on an actual domain, or locally stored in a sub-directory of the server root?

mod rewrite rule for parameters

I have below url(s)
www.localhost.com/profile.php?username=first.last
i would like to permanently redirect above url to using .htaaccess file. (apache server)
www.localhost.com/first.last
please also consider there are few other urls there but i dont want to touch them..like
www.localhost.com/message.php?id=12
www.localhost.com/editprofile.php?editname=first.last
www.localhost.com/uploadphoto.php?username=first.last
can anyone please help me.
thank you in advance.
You could try to handle the Query String with RewriteCond and pass the captured match to RewriteRule. You must exclude any .phpscripts of your rewriting rule otherwise it will create some problems with others URLs.
Don't forget to add the [QSA] tag after your RewriteRule otherwise it will not add the Query String parameters.
Maybe doing something like this:
RewriteEngine on
#serve any existing php scripts as usual
RewriteRule ^([a-zA-Z0-9]+\.php) - [L]
#and now handle your specific stuff:
RewriteCond %{QUERY_STRING} ^([a-zA-Z0-9]+\./[a-zA-Z0-9]+)$
RewriteRule ^(%1)$ /profile.php?username=%1 [QSA]
I don't test it but it should be a good beginning. You can read some good stuff here and inside the docs for mod_rewrite httpd 2.2 about how to write and handle specific rewriting use cases.

using mod_rewrite to point at a blog

I'm trying to use mod_rewrite to point the blog portion of a site to a blog site.
this is what I have to handle the normal stuff
RewriteRule ^(\w+)/?$ index.php?page=$1
This is what i'm trying to use for the blog site
RewriteRule ^blog/?$ http://url.to.my.blogger.site
but it's not working, when I go to site/blog it directs me to index.php?page=blog is there something I need to do to not do the second rewrite if the first is correct? like an if/else? sorry don't know much about mod_rewrite so any advice would be awesome.
also I noticed that if I try to do something like site/home everything works fine but if I attempt to hit site/home/ it puts all of my urls into the wrong context, for example my css and images don't get loaded correctly.
my full file is this
RewriteEngine on
RewriteRule ^blog/?$ remote/blog/uri/here
RewriteRule ^(\w+)/?$ index.php?page=$1
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php - [F]
and when i hit site/blog it still tries to serve index.php?page=blog, I'm guessing I have to break out of the code at some point? I couldn't find documentation on if/else statements
I needed to add flags to my RewriteRule lines so that the server wouldn't evaluate further. Changing them to be
RewriteRule ^/blog http://url.to.blog [L]
did the trick, the problem was that it was evaluating all the way down, seeing as I wasn't attempting to go to index the last valid rule to evaluate was the general rewrite rule.

Resources