Rewrite Urls with question mark and ampersand - .htaccess

On my website I have categories with urls like so
www.example.com/?foo and www.example.com/?bar
In these categories I have articles with urls like this:
www.example.com/?foo&nr=12
and groups with urls like this:
www.example.com/?foo&group=somegroup
I am trying to rewrite
/foo to /?foo, /bar to ?bar etc.; /bar/12 to /?bar&nr=12 etc. and /foo/somegroup to /foo&group=somegroup
It should work both with and without final slash.
The (admittedly clueless) things I tried either did nothing or resulted in a server error. At some points I also tried just getting the &nr=xx bit or simply the categories. Some of them are:
RewriteCond %{QUERY_STRING} (?:^|&)([^&]+) RewriteRule /?(.*) %1/$1
[QSA]
RewriteRule ^/([^/]+)/([^/]+)$ /?$1&nr=$2
RewriteRule ^/([^/]+) ?nr=$1 [L]

Ok, assuming the file handling the queries is index.php and foo and bar don't change to something else, you could start like this:
RewriteEngine On
RewriteBase /
RewriteRule ^foo$ foo/ [NC,R]
RewriteRule ^foo/$ index.php?foo [NC,L]
RewriteRule ^foo/([A-Za-z0-9_-]+)$ foo/$1/ [NC,R]
RewriteRule ^foo/([A-Za-z0-9_-]+)/$ index.php?foo&group=$1 [NC,L]
Now you should be able to do the bar part on your own!
Btw. if you really want your query string to only be ?foo (without a value, like so: ?foo=somevalue) you'll have to check for foo's existence differently than you would for e.g. the somegroup part.

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.

Remove certain part of URL

I have a website where i have pages like:
domain.com/?p=settings
domain.com/?p=daily
And i am looking for rewrite that ?p= part, so it would be like
domain.com/settings
So far i have tried to add this to htaccess files:
RewriteRule ^([^\.]+)$ $1?p= [NC,L]
but it did not worked.
Also I have tried look from Google but could not find any.
I have tried other RewriteRule's but they did not work either.
RewriteRule does not include query string. It is available as a separate variable enter link description here
The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path.
So the following won't work.
RewriteRule ^([^.]+)$ $1?p= [NC,L]
You need something like
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/ %2?
Checkout Apache Mod ReWrite Wiki and scroll down to "Making the Query String Part of the Path"
You were close with your attempt, you need to use this in your .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /?p=$1 [L]
Make sure to clear your cache before testing this.

.htaccess change /es/ to .es

I am working with a domain like:
http://www.domain.com/es/blabla.html
And I want to change the /es part for .es and convert the URLS to something like:
http://www.domain.com.es/blabla.html
I've been trying lots of possibilities but I haven't reached a solution.
For example one of them has been:
RewriteRule ^es/(.*)$ http://www.domain.com.es/$1 [L]
But it has entered in an infinite loop. Which I've tried to avoid adding this lines:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
But the server doesn't allow this calls. So I still have the problem.
Check this.
RewriteEngine On
RewriteRule ^\.html$ /es/blabla.html [L]

htaccess mod rewrite $_GET to variables with slashes

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.

mod_rewrite so that first-level subdirectory is a GET variable

Alright, title is REALLY sloppy.
Here's my problem: I have a news site and when you go to the main page (domain.com) it redirects you to domain.com/news/top?geography=San_Francisco after it figures out your geography.
How do I use the .htaccess so that it goes from domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top ?
There are some similar questions, but I have not found one similar enough in that you're editing the URL as a furtherback subdirectory.
It should also be noted that I am using the Code Igniter framework for PHP and it normally has it as domain.com/index.php/news/top?geography=San_Francisco but I did a mod_rewrite already to get rid of the index.php. The code is as follows for that:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Code I've tried:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Before the index.php rule that you have, try adding this:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
You'll need to make sure the links you generate are in the form of domain.com/San_Francisco/news/top though.
But to take care of the links in the wild that still look like the old way, you have to match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]
This will 301 redirect the browser if someone goes to the link domain.com/news/top?geography=San_Francisco and make it so the browser's address bar says this: domain.com/San_Francisco/news/top. At which point the browser will send another request for the second URL, and you use the rule above to change it back into the one with a query string.

Resources