.htaccess URL rewrite to point to ajax file - .htaccess

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]

Related

Rewrite htaccess url and remove extension

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]

htaccess - remove segment of url with redirect

Need to use .htaccess to redirect all urls that have blog-entry in path like this:
http://example.com/blog/blog-entry/blog-title
to this:
http://example.com/blog/blog-title
Tried this per another stack answer, but no luck:
RewriteRule ^blog-entry/(.*)$ $1 [L,R=301,QSA]
Please advise.
Change your rule with this rule:
RewriteCond %{THE_REQUEST} \s/+(.*)/blog-entry/(\S*) [NC]
RewriteRule ^ /%1/%2 [R=301,NE,L]
Since we're using THE_REQUEST here, this rule will work from site root or /blog/ directory.

php - Htaccess as fake directories

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]

using htaccess to fix url mistake

So launched a site and made a mistake with a link and did not structure it correctly. As quite a few of these links have been picked up in Google I need to redirect the wrong links permanently without affecting the correct links and am not sure how to do it properly.
My links should be like this: /compare/itemname.html
But with the mistake they are appearing like this in Google:
/compare/itemname (without the .html part)
this was only because I missed off the .html when the links are created
dynamically and have fixed this bit now.
This is in my htaccess and works fine:
RewriteCond %{THE_REQUEST} \ /+index\.php\?keyword=([^&\ ]+)
RewriteRule ^ /compare/%1.html? [L,R]
RewriteRule ^compare/([^/]*)\.html$ /index.php?keyword=$1 [NC,L]
So how do I change from this /compare/itemname to /compare/itemname.html
where itemname can change - without messing up the above code in the
.htaccess
Or is it better at this stage just to do a 301 redirect and again how
would I pick out the urls itemname that is missing the .html ?
Thanks for any assistance
You can add a new redirect rule to add .html:
RewriteCond %{THE_REQUEST} \ /+index\.php\?keyword=([^&\ ]+)
RewriteRule ^ /compare/%1.html? [L,R]
RewriteRule ^(compare/[^.]+)/?$ /$1.html [NC,L,R]
RewriteRule ^compare/([^.]+)\.html$ /index.php?keyword=$1 [NC,L,QSA]
Try this .htaccess in your /compare/ dir
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*[^/])$ http://www.example.com/compare/$1\.html [L,R=301]

Remove file name in a URL

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]

Resources