htaccess mod rewrite $_GET to variables with slashes - .htaccess

I would like to rewrite URL's with htaccess to better readable URL's and use the $_GET variable in PHP
I sometimes make use of a subdomain so it has to work with and without. Also are the variables not necessary in the url. I take a maximum of 3 variables in the URL
the URL sub.mydomain.com/page/a/1/b/2/c/3 should lead to sub.mydomain.com/page.php?a=1&b=2&c=3 and the url sub.mydomain.com/a/1/b/2/c/3 should lead to sub.mydomain.com/index.php?a=1&b=2&c=3 where $_GET['a'] = 1
I came up with this after searching and trying a lot
RewriteEngine on
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4&$5=$6&$7=$8 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3&$4=$5&$6=$7 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4&$5=$6 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3&$4=$5 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)$ $1.domain.com/$2.php [L,QSA,NC]
but what I get is an not found server error
I'm not that good at this so maybe I oversee something.
Also I would like it to work with and without a slash at the end
Should I make use of RewriteCond and/or set some options?
Thanks in advance.

When using RewriteRule, you don't include the domain name in the line. Also, make sure you turn on the RewriteEngine first. Like this:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)$ index.php?$1=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?$1=$2&$3=$4
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?$1=$2&$3=$4&$5=$6
The first line will rewrite sub.mydomain.com/a/1 to sub.mydomain.com/page.php?a=1, the second rewrites sub.mydomain.com/a/1/b/2 to sub.mydomain.com/page.php?a=1&b=2, and so on.

Related

how to replace "?" and "=" sign with "/" in URL - PHP htaccess

i am working on project, which is running XAMPP localhost and PHP MYSQLI,
my question : how i replace "?","=" signs with "/" slash. ?
like, my url is "archive?date=2017-06-02&p=4"
and i want to force it "archive/2017-08-02/4"
i found many codes on stackoverflow and some other sites, but that are not working for me.
if codes are working then, CSS files and GET method doesn't work on my project.
complete code of .htaccess is given below.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^=]*)=([^=]*)=(.*) /$1/$2/$3 [N]
RewriteRule ^([^=]*)=([^=]*)$ $1/$2 [L]
RewriteRule ^home index.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^about about.php [NC,L]
RewriteRule ^article article.php [NC,L]
RewriteRule ^news news.php [NC,L]
RewriteRule ^video videos.php [NC,L]
RewriteRule ^video?vid=([0-9]+) videos.php?q=$1 [NC,L]
RewriteRule ^article?num=([0-9]+) article.php?num=$1 [NC,L]
RewriteRule ^editorial?num=([0-9]+) editorial.php?num=$1 [NC,L]
RewriteRule ^news?news=([0-9]+) news.php?news=$1 [NC,L]
You cannot check against the query string in a rewrite rule. You need rewrite conditions for that:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2?
Demo here: http://htaccess.mwl.be?share=81e85c09-d505-5206-ab14-6c5059107808
If you want to actually redirect just add [R=301,L] to the end of the RewriteRule.
However, looking at the above I suspect you have your script sitting listening at /archive/index.php?data=foo&p=bar but want URLs to be like /archive/date/p, ie pretty.
This is actually a very common misconception about how htaccess URL rewrites work when you first get into them.
RewriteRules will mask or redirect URLs for you but they cannot change the underlying location a script is located at and thus the address used to pass it information.
In other words - you can mask /archive/index.php?data=foo&p=bar as /archive/date/p so that requests made to /archive/date/p resolve to /archive/index.php?data=foo&p=bar, but you cannot make it so that if you enter /archive/index.php?data=foo&p=bar as URL you have the URL change to /archive/date/p while still serving content from /archive/date/p. It has to be either or.
If this all sounds about right my advice would be as follows:
First, put your code into a different file, say /archive/script.php.
Next add the following to your htaccess:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2? [R=301,L]
RewriteRule ^archive/([^/]+)/([^/]+) /archive/script.php?date=$1&p=$2
Note that the first two lines are the same as before, but now there is a new line that looks for the masked URL format of /archive/date/p and sends it off to the actual script, which is handled by the new RewriteRule.
The behaviour of the new rule is demoed here: http://htaccess.mwl.be?share=06751667-f16f-5c13-91eb-dd5cffdc6db3
Hope this makes sense / helps.

Troubleshooting mod_rewrite in a .htacces with a LightSpeed

I have a couple web pages located at these locations:
Home Page / Index : www.codeliger.com/index.php?page=home
Education : www.codeliger.com/index.php?page=home&filter=1
Skills: www.codeliger.com/index.php?page=home&filter=2
Projects: www.codeliger.com/index.php?page=home&filter=3
Work Experience: www.codeliger.com/index.php?page=home&filter=4
Contact : www.codeliger.com/index.php?page=contact
I am trying to rewrite them to prettier urls:
codeliger.com/home
codeliger.com/education
codeliger.com/skills
codeliger.com/projects
codeliger.com/experience
codeliger.com/contact
I have confirmed that my htaccess file works and mod-rewrite works to google, but I cannot get my syntax working that was specified in multiple tutorials online.
RewriteEngine on
RewriteRule /home /index.php?page=home
RewriteRule /([a-Z]+) /index.php?page=$1
RewriteRule /education /index.php?page=home&filter=1
RewriteRule /skills /index.php?page=home&filter=2
RewriteRule /projects /index.php?page=home&filter=3
RewriteRule /experience /index.php?page=home&filter=4
How can I fix my syntax to rewrite these pages to prettier urls?
The first thing you should probably do is fix your regex. You cannot have a range like [a-Z], you can just do [a-z] and use the [NC] (no case) flag. Also, you want this rule at the very end since it'll match requests for /projects which will make it so the rule further down will never get applied. Then, you want to get rid of all your leading slashes. Lastly, you want a boundary for your regex, otherwise it'll match index.php and cause another error.
So:
RewriteEngine on
RewriteRule ^home /index.php?page=home
RewriteRule ^education /index.php?page=home&filter=1
RewriteRule ^skills /index.php?page=home&filter=2
RewriteRule ^projects /index.php?page=home&filter=3
RewriteRule ^experience /index.php?page=home&filter=4
RewriteRule ^([a-z]+)$ /index.php?page=$1 [NC]

Can these .htaccess mod_rewrite rules be compacted?

I have a few mod_rewrite rules in my htaccess files to redirect inexistent pages to a default page, like this:
http://domain.tld/category1/subcategoryX
http://domain.tld/category1/subcategoryX/
http://domain.tld/category1/subcategoryX/property1
http://domain.tld/category1/subcategoryX/property1/
all redirected to
http://domain.tld/category1/subcategoryX/property1/value1
I'm doing this redirection because the subcategory and property have no page, only the values do.
These are the rules:
RewriteRule ^category1/[a-z-]+$ http://%{HTTP_HOST}%{REQUEST_URI}/property1/value1 [R,NC,L]
RewriteRule ^category1/[a-z-]+/$ http://%{HTTP_HOST}%{REQUEST_URI}property1/value1 [R,NC,L]
RewriteRule ^category1/[a-z-]+/property1$ http://%{HTTP_HOST}%{REQUEST_URI}/value1 [R,NC,L]
RewriteRule ^category1/[a-z-]+/property1/$ http://%{HTTP_HOST}%{REQUEST_URI}value1 [R,NC,L]
The rules are working, but they're kinda verbose. Is there any trick to compact the rules? The difference between the first two is just the end slash, and so is for the last two.
RewriteRule ^(category1/[a-z-]+)/?$ /$1/property1/value1 [R,NC,L]
RewriteRule ^(category1/[a-z-]+/property1)/?$ /$1/value1 [R,NC,L]
or maybe you could even redirect all url starting with category1/*
RewriteRule ^(category1/[a-z-]+) /$1/property1/value1 [R,NC,L]

IIS Mod-Rewrite QUERY_STRING/QSA problems

We use IIS Mod-Rewrite from MicoNovae for our IIS rewrites on Windows 2003.
We use the RewriteRule command, for example:
http://www.site.com/section35/page1/tiling-tools/
becomes:
http://www.site.com/search.asp?section=35&page=1&model=tiling-tools
I now have a situation where I need to append the querystring to the re-written URL, for example:
http://www.site.com/section35/page1/tiling-tools/?myid=dskajh34kjhsvkjh34
need to become
http://www.site.com/search.asp?section=35&page=1&model=tiling-tools&myid=dskajh34kjhsvkjh34
My rules are:
RewriteEngine On
RewriteRule /\.htaccess$ - [F]
RewriteRule ^/section([^/]+)/page([^/]+)/?([^/]*)/?([^/]*)/?([^/]*)/$ /search_results.asp?section=$1&page=$2&model=$3 [L]
I've tried the following, without success:
RewriteRule ^/section([^/]+)/page([^/]+)/?([^/]*)/?([^/]*)/?([^/]*)/$ /search_results.asp?section=$1&page=$2&model=$3&%{QUERY_STRING} [L]
RewriteRule ^/section([^/]+)/page([^/]+)/?([^/]*)/?([^/]*)/?([^/]*)/$ /search_results.asp?section=$1&page=$2&model=$3 [L,QSA]
Suggestions will be appreciated ('cos it's driving me mad!)
Thank you!
Apart from the query string appending, I'm not even sure your rewrite rule is working at all?!? I would suggest to use something like this:
RewriteRule ^/section([0-9]+)/page([0-9]+)/(.*)/ /search_results.asp?section=$1&page=$2&model=$3 [NC,L,QSA]
The NC flag makes your rewrite rule non-case sensitive and the QSA flag adds the query string you need.

.htaccess redirect from GET variable to url string

I need to redirect
/search?keywords=somesearchterm
to
/search/somesearchterm
This seems incredibly basic but I've been pounding my head against it for an hour.
Thanks for taking the time to look at this.
You want to implement what is called a "301 Redirect" with mod_rewrite.
RewriteEngine ON
RewriteRule ^/search\?keywords=somesearchterm$ /search/somesearchterm
adding regular expressions:
RewriteEngine ON
RewriteRule ^/search\?keywords=(.+) /search/$1 [R=301,L]
R=301 means provide a 301 Header redirect so the user's URL changes in the browser, and L means don't process any more rewrite rules if this one matches.
If you want to do the reverse -- in other words, if someone goes to mysite.com/search/asearchterm and you want the URL to stay the same, but "behind the scenes" you want it to load a certain server script, do this:
RewriteEngine ON
RewriteRule ^/search/(.+) /search.php\?keywords=$1 [L]
You can not match aginst Query string in RewriteRule directive. Use %{THE_REQUEST} or %{QUERY_STRING} server variables to match the Query string :
The following rule works fine for this redirection
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/search\?kewords=([^&\s]+) [NC]
RewriteRule ^ /search/%1? [NE,NC,R,L]
RewriteRule ^search/([^/]+)/?$ /search?keyword=$1 [QSA,NC,L]

Resources