htaccess redirect url to directory file - php

am working on making my site urls search engine friendly and for which am rewriting urls with GET parameters and so i have done rewriting but now htaccess is not pointing that url to php file which is suppose to handle the url
my old url
www.domain.com/foo/myfile.php?nid=554
new rewritten url with php
www.domain.com/foo/554-demo-page-title
my current htaccess rules which work for old urls but not for new
Options +FollowSymLinks
RewriteEngine On
#RewriteCond %{SCRIPT_FILENAME} !-d
#RewriteCond %{SCRIPT_FILENAME} !-f
RewriteBase /
RewriteRule ^([0-9]+)-(.*) ./foo/myfile.php?nid=$1 [NC]
so i want to that both old and new urls land on /foo/myfile.php becuase myfile.php can handle both urls incase of old url it rewrite and redirect as new url , i played for few hours with htaccess rules but no success

You can use this rule in site root .htaccess (assuming there is .htaccess inside foo/ directory):
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^foo/(\d+)-.*$ foo/myfile.php [L,QSA,NC]
If you already have /foo/.htaccess then use this rule inside that file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /foo/
RewriteRule ^(\d+)-.*$ myfile.php [L,QSA]

Related

htaccess for url containning specific word

I have a website with URLs like /product-category/clothing/accessories/?filter_color=yellow now I want to rewrite these kinds of URLs to /index.php/product-category/clothing/accessories/?filter_color=yellow in fact, I want to add a rewrite rule in my htaccess that if the URL contains product-category and does not contain index.php then rewrite the index.php at the beginning of the URL. For this purpose I have developed the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} product-category
RewriteRule . /index.php [L]
</IfModule>
But it does not work correctly. First, it adds index.php to all of the URLs and then it adds an extra / when /index.php already exists in the URL
Here is the complete .htaccess file:
htaccess
You may use this rule:
RewriteRule ^product-category/.* /index.php/$0 [L,NC]

Remove directory name from URL using htaccess

I have bunch of folders and files in folder "front". I want to remove "front" from url. I write htacess but it gives 404 error.
RewriteEngine On
RewriteBase /
RewriteRule ^front/(.*)$ /test/$1 [L,NC,R]
I want to rewrite url like this :
http://localhost/test/front/abc/abc.php
to
http://localhost/test/abc/abc
You have it back to front
RewriteEngine On
RewriteBase /
RewriteRule ^test/(.*)$ /test/front/$1 [L,NC,R]
Try adding this to the htaccess file in your document root, preferably above any rules you may already have there:
RewriteEngine On
RewriteRule ^test/(.*)$ /front/$1 [L,R=301]

URL Rewriting (Internal/External Forwarders) .htaccess

I am using .htaccess for URL Rewriting. My current .htaccess code is:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+fr/transporter/transporterPublicProfile\.php\?profil=([^\s&]+) [NC]
RewriteRule ^ fr/profil-des-transporteurs/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^fr/profil-des-transporteurs/([^/.]+)/?$ fr/transporter/transporterPublicProfile.php?profil=$1 [L,QSA,NC]
My URL is :
https://example.com/fr/profil-des-transporteurs/1927
It works fine when I place .htaccess file in DOCUMENT_ROOT directory.
But it doesnot work when I put same .htaccess file in real sub-directory i.e /fr/.
So what should I do, if I want to place .htaccess file in sub-directory i.e /fr/
Use these rules inside /fr/.htaccess:
RewriteEngine On
RewriteBase /fr/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /fr/transporter/transporterPublicProfile\.php\?profil=([^\s&]+) [NC]
RewriteRule ^ profil-des-transporteurs/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^profil-des-transporteurs/([^/.]+)/?$ transporter/transporterPublicProfile.php?profil=$1 [L,QSA,NC]
Try this
RewriteEngine On
RewriteBase /fr

.htaccess url rewrite not formatting properly

Currently I working through my localhost in MAMP. I have read and research for the proper way to allow .htaccess file url rewrite and was successful. But now, the file is not formatting the links at all as desired. For example I have three pages index.php, about.php and contact. I am using MVC for the frame working of my site. Is this a problem with the code in the .htaccess file or my localserver?
Currently links look like this when going from one page to another:
localhost/mvc/index.php?
localhost/mvc/index.php?p=about
localhost/mvc/index.php?p=contact
.htaccess should format the links to look like this:
localhost/mvc/index/?
localhost/mvc/about/?
localhost/mvc/contact/?
.htaccess:
<IfModule mod_rewrite.c>
# Turn on the engine:
RewriteEngine on
# Set the base to this directory:
RewriteBase /mvc/
# Redirect certain paths to index.php:
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
</IfModule>
Replace your existing .htaccess code fully with this code:
Options +FollowSymLinks -MultiViews
DirectoryIndex index.php index.html
# Turn mod_rewrite on
RewriteEngine On
# Set the base to this directory:
RewriteBase /mvc/
# Redirect /mvc/index.php?p=page to /mvc/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+mvc/index\.php\?p=([^\s]+) [NC]
RewriteRule ^ %1/? [R=302,L]
# Redirect /mvc/index.php to /mvc/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+mvc/index\.php\s [NC]
RewriteRule ^ /mvc/ [R=302,L]
# Internally forward certain /mvc/page/ to /mvc/index.php?p=page
RewriteRule ^(about|contact|this|that|search)/?$ /mvc/index.php?p=$1 [L,QSA,NC]
Replace
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
with :
RewriteRule ^(about|contact|this|that|search)/\?$ index.php?p=$1
I just escaped the ? with a slash because, otherwise, you don't do this, the question mark will be interpreted and will means that the slash before it is optinal.

How does Wordpress rewrite the URL without a rewrite map?

The .htaccess file for a wordpress site looks something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
...and there is no rewrite map set in htdocs. How does this work? How does Apache know how to rewrite these url?
The Apache does not know. All the requests are sent to index.php and Wordpress keeps an internal log of which page to redirect where, and it redirects it. So, in essence, Wordpress actually has two sets of rewrite rules, one internally and a "greedy" external rule in your .htaccess which basically makes all requests refer to the internal rewrite rules.
You may be interested in using this plugin which shows all the internal rewrites that Wordpress is doing itself.

Categories