Here are my current URL:
?p=new-homes-by-east-homes&id=416&name=east-homes
Here is how I want to have:
?p=new-homes-by-east-homes&id=416
So far, I have generated this way using .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^p=new-homes-by-([^/]+)&id=([0-9]+)?$ ?p=new-homes-by-place&id=$2&name=$1 [L]
This actually works somehow, however, I miss the question mark on front of the new URL ?p=new....
When I put the question mark, it jump me to the homepage.
You can't match against the query string in the pattern of a RewriteRule. You need to match against the %{QUERY_STRING} var in a RewriteCond then use the % backreferences:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^p=new-homes-by-([^/]+)&id=([0-9]+)?$
RewriteRule ^/?$ /?p=new-homes-by-%1&id=%2&name=%1 [L]
Related
I have this htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
DirectoryIndex index.php
RewriteRule ^doit$ doit.php [L]
RewriteRule ^([\$\.A-Za-z0-9_-]+)$ follow.php?follow=$1 [QSA,L]
But when i try to go in the index page, everytime showing follow.php page, even doit page show this last one.
Any fix for this issue please ?
Thank's.
RewriteCond technically applies only to the first RewriteRule, so
please try using something like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([\$\.A-Za-z0-9_-]+)$ follow.php?follow=$1 [QSA,L]
#RewriteConds for this rule
RewriteRule ^doit$ doit.php [L]
DirectoryIndex index.php
I hope be useful. Regards.
I have the following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([0-9]+)/([a-zA-Z0-9]+)/(.+)?$ project.php?id=$1&pName=$2&urlPram=$3 [L,NC]
</IfModule>
when my url format is something like:
mysite.com/project/1/test/vw
everything works fine. But I would also like when the url is typed in the other format, to be rewritten to the first format.
i.e. When I type the url like this:
mysite.com/project.php?id=1&pName=test&urlParam=vw
I wants it to be turned into:
mysite.com/project/1/test/vw
Do I have to write another rule? or is it accomplished in some other way?
The current rewrite rule would not handle both the cases. Add the below rewrite rule as well.
RewriteEngine On
RewriteBase /mysite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([0-9]+)/([a-zA-Z0-9]+)/(.+)?$ project.php?id=$1&pName=$2&urlParam=$3&no-redir [L,NC,QSA]
RewriteCond %{QUERY_STRING} !no-redir
RewriteCond %{QUERY_STRING} (^|&)id=([^&]+)&pName=([^&]+)&urlParam=([^&]+)
RewriteRule ^project\.php$ project/%2/%3/%4? [R=301,L,NC]
You need one more external redirect rule. Use this code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} /project\.php\?id=([^&]+)&pName=([^&]+)&urlPram=([^&\s]+) [NC]
RewriteRule ^ project/%1/%2/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([0-9]+)/([a-zA-Z0-9]+)/(.+)?$ project.php?id=$1&pName=$2&urlPram=$3 [L,NC,QSA]
</IfModule>
I'm trying to rewrite some dynamic page url in which data is taken from database with slug like "this-is-a-link".
Options +FollowSymlinks
RewriteEngine On
RewriteBase /testweb/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ faq_pages.php?page=$1 [QSA,L]
It works fine with http://www.test.com/testweb/this-is-a-link
But I need to change the rewrite rule based on any link clicked as below.
http://www.test.com/testweb/faq/this-is-a-link, 'testweb/news/this-is-another-link','/testweb/testimonials/test-link'
The "faq_pages.php" in the above RewriteRule should be changed to "news_pages.php". How can I have it dynamically so that I can use for every page with same htaccess or how can I check and rewrite the value to different pages in htaccess.
Also please advice me how to correct below code.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ faq_pages.php?page=$1
I need to show html urls in the address bar like '/testweb/this-is-a-link.html' or '/testweb/faq/this-is-a-link.html'
Edit: By the help of Jon Lin I have updated my codes as below.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /testweb/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^faq/(.*?)/?$ faq_pages.php?page=$1 [QSA]
Now both below conditions are working :
/testweb/faq/another-faq-test.html,
/testweb/faq/another-faq-test.html/
As I don't have any sub-folder with name 'faq' the template files(images/css/js etc) in root folder are not loading.
How can I fix it?
Thanking you
Are you looking for something like this?
Options +FollowSymlinks
RewriteEngine On
RewriteBase /testweb/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^faq/(.*)$ faq_pages.php?page=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^news/(.*)$ news_pages.php?page=$1 [QSA,L]
Is it possible to create a pretty url without having to touch the file itself? For example:
http://sawebdev01/company/news-events/press-releases/press-release/?pr=168#.Uajh_kCyBSK
And convert it to:
http://sawebdev01/company/news-events/press-releases/pr/168
I tried this, and it does not seem to work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} /company/news-events/press-releases/press-release/?pr=([^&]+) [NC]
RewriteRule .* /company/news-events/press-releases/press-release/pr/%1? [R=301,L]
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s(/company/news-events/press-releases)/press-release/\?pr=([^&]+) [NC]
RewriteRule ^ %1/pr/%1? [R=302,L]
RewriteRule ^(company/news-events/press-releases)/pr/([^/]+)/?$ /$1/press-release?pr=$2 [L,NC,QSA]
I have a .htaccess that sends these urls:
http://example.com/this/is/test/
to index.php that gets the url like this:
index.php?var1=this&var2=is&var3=test
All I want to do is change the / to :means the url will be like this:
http://example.com/this:is:test
that sends the variables to index page just like before. Here's my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)?/?([^/]+)?/?([^/]+)?/? [NC]
RewriteRule .* index.php?var1=%1&var2=%2&var3=%3 [L]
You shouldn't use %{REQUEST_URI} to match parts. So, in order to make what you want you have to change your / to :. I'll also use a better syntax.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^:]+):([^:]+):([^/]+)/? index.php?var1=$1&var2=$2&var3=$3 [L]