How can i remove a file name in url with mod_rewrite.
ex: searchpage-some+search.html in to some+search.html
This is my .htaccess code code.
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /searchpage\.php\?search=(.*)\ HTTP
RewriteRule ^searchpage\.php$ /searchpage-search-%2.html? [R,L]
#Internal rewrite
RewriteRule searchpage-search-(.*)\.html$ searchpage.php?search=$1 [L]
This is for a search form that uses $_GET request. This works well only thing is that i want to remove the file name. I would really appreciated if anyone can help out.
In your example you have searchpage-some+search.html but in your htaccess you are actually rewriting it to searchpage-search-some+search.html.
But to remove the searchpage-search from links, just remove searchpage-search- from the RewriteRule. Your htaccess would be something like
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /searchpage\.php\?search=(.*)\ HTTP
RewriteRule ^searchpage\.php$ /%2.html? [R,L]
#Internal rewrite
RewriteRule (.*)\.html$ searchpage.php?search=$1 [L]
Related
I'm trying to rewrite and remove extension
http://www.example.com/pages/cart.php
to
http://www.example.com/cart
My htaccess file is located in the public folder.
RewriteEngine On
RewriteRule (.*) pages/$1 [L]
With your shown attempts, please try following htaccess Rules. Make sure you keep your htaccess file along with pages folder(not inside it).
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/pages/(cart)\.php\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^(.*)/?$ pages/$1.php [L]
I am making a mini blog that could make it's url looks like this:
From: http://127.0.0.1/index.php?post=the-story-of-us
To: http://127.0.0.1/view/the-story-of-us
I have tried this but i'm getting 404 not found.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?post=([^&]+)
RewriteRule ^ /view/%2/? [L,R=301]
Your current rule only handles the case: Redirect old url to new url.
(By the way, +1 for using THE_REQUEST to avoid a redirect loop)
You also need to handle the case: Rewrite (internally) new url to old url.
Here is how your htaccess should look like
RewriteEngine On
# Redirect /index.php?post=XXX to /view/XXX
RewriteCond %{THE_REQUEST} \s/index\.php\?post=([^&\s]+)\s [NC]
RewriteRule ^ /view/%1? [L,R=301]
# Internally rewrite back /view/XXX to /index.php?post=XXX
RewriteRule ^view/([^/]+)$ /index.php?post=$1 [L]
I do not udnerstand your RewriteCondition, but the RewriteRule should look like this:
RewriteEngine on
RewriteBase /
RewriteRule ^view/(.*)/? ./index.php?post=$1 [L,R=301]
I'm struggling to add a rewrite rule to my .htaccess file.
I need the URL stings in this format:
/myFile.ajax
to point to this:
/include/dir/ajax.myFile.php
Here's where I am:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+include/dir/ajax\.([^.]+)\.php[/\s?] [NC]
RewriteRule ^ /%1\.ajax? [R=302,L]
Not sure what I'm missing.
Try also adding:
RewriteRule ^([^/]+)\.ajax$ /include/dir/ajax.$1.php [L]
I was able to get my old URL's with index.php to redirect to my new clean URL's
eg: example.com/index.php?gender=m&height=70&weight=150
to
example.com/men/70-inches/150-lbs/
However I've been trying to hack my way getting the other URL's without the index.php to also redirect to the clean url
eg: example.com/?gender=m&height=70&weight=150
to
example.com/men/70-inches/150-lbs/
my current redirection for URL's with index.php that works looks like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?gender=m&height=([0-9-]+)&weight=([0-9-]+)
RewriteRule ^index\.php$ /men/%1-inches/%2-lbs/? [L,R=301]
I've tried this, for URL's without index.php but it doesn't work:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?gender=m&height=([0-9-]+)&weight=([0-9-]+)
RewriteRule ^/$ /men/%1-inches/%2-lbs/? [L,R=301]
Problem is google has indexed a bunch of my page with the example.com/?gender=m&height=70&weight=150 and I want to have them all pointing to the one clean URL.
You have the correct idea, but your rewrite rule's pattern won't work: ^/$. The URI's sent through rules in an htaccess file has the leading slash removed, so there's no / URI ever, it'll just be blank:
# no slash---v
RewriteRule ^$ /men/%1-inches/%2-lbs/? [L,R=301]
So the whole thing should be:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?gender=m&height=([0-9-]+)&weight=([0-9-]+)
RewriteRule ^$ /men/%1-inches/%2-lbs/? [L,R=301]
I'm trying to learn Regex & URL Rewriting in PHP. How can I create an .htaccess file which will rewrite (not redirect) any url to index.php?q={url}?
For example:
http://www.example.com/baloon
to
http://www.example.com/index.php?q=baloon
I tried:
RewriteEngine On
RewriteRule ^$ index.php?q=$1 [R]
...but it is not working. What am I doing wrong?
Thanks.
Rewriting ANY url to index.php?q=$1 would result in internal server error as it'd create an endless loop; instead do something like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ index.php?q=$1 [L]