Remove question mark from URL - apache

I don't have much experience with htaccess and mod_rewrite or anything, but how would one remove the ? from a URL that looks something like: site.com/?page so it looks like site.com/page ?
I'm trying this at the moment:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
But it just redirects everything to just site.com index page, without anything else in the url.

Your backreference is incorrect. You want to match the 2nd grouping from %{THE_REQUEST}, not the first (which is index.php). So your first rule should look like:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%2? [L,R=301]
# 2 here--------^
The 2nd grouping would be the ([^&\ ]+) match.

Related

RewriteRule - remove params keys and keep values in address bar

RewriteEngine ON
RewriteRule ^video$ video.php [L]
the above line works
example.com/video is interpret as example.com/video.php
now I need example.com/video?id=5&s=lorem-ipsum
to interpret as example.com/video/5/lorem-ipsum - and vice versa
RewriteCond %{THE_REQUEST} video?id=([^\s&]+)&s=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?id=$1&s=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?/$1/$2 [L,QSA]
doesn't work - in both directions
pls help
With your shown attempts, please try following .htaccess rules file. These rules are assuming you want to infernally rewrite to index.php you can change php file name in 2nd set of rules as per your requirement.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect Rules from here..
##Redirect 301 to example.com/video/5/lorem-ipsum from example.com/video?id=5&s=lorem-ipsum
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(video)\?id=([^&]*)&s=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Internal rewrite for example.com/video?id=5&s=lorem-ipsum
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^video/([^/]*)/(.*)/?$ index.php?id=$1&s=$2 [QSA,NC,L]

.htaccess rewrite subfolders to remove /index

I have some htaccess rules which rewrite friendly URLs. Everything is working apart from one thing.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
This is a site with language subfolders, e.g.:
/fr/about.html
/en/about.html
With the above htaccess rules these urls come out like:
/fr/about
/en/about
However this also means my index pages URLs look like:
/fr/index
/en/index
What I would like, is for /index the URL looks like
/fr
/en
Which would mean they respect the trailing slash part of my above htaccess but remove index from the URL.
Any help much appreciated!
Try to add (before RewriteCond %{THE_REQUEST} ^...):
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*?)/index(\.html)?[\s?] [NC]
RewriteRule ^ %1 [R=301,L]

RewriteRule changing the URL instead of mapping to a file

I've encounter a weird behavior that I cannot explain nor correct. I need to redirect every HTTP request to HTTPS. I've use the following code :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# The query string in the rewrite is for testing purposes
RewriteRule (.*) /index.php?url=$1&%{REQUEST_URI}&http=%{HTTPS} [L]
So far, it works. Then, I need a single page to be HTTP, so I added some rewrite conditions :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/not-https
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/not-https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?url=$1&%{REQUEST_URI}&https=%{HTTPS} [L]
Now, here what's happening. For some reasons, when accessing the /not-https page, it redirects to /index.php?url=not-https&/not-https&https=off
Here is a map of GET requests followed by the redirects / displayed URL.
GET: http://example.com/test
-> https://example.com/test with proper $_GET
GET: http://example.com/test.jpg
-> https://example.com/test.jpg with no $_GET (file exists)
GET: https://example.com/not-https
-> http://example.com/not-https
-> http://example.com/index.php?url=not-https&/not-https&https=off
My question is why does the not-https change the displayed URL (and therefor, mess up my application)?
It is happening because value of REQUEST_URI variable is changing to /index.php?... in last rule that makes condition !^/non-https succeed in 2nd rule and makes it execute that rule.
Change your 1st 2 rules to this:
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+not-https [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+not-https [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Unlike REQUEST_URI variable THE_REQUEST doesn't change it's value after execution of other internal rewrites.

.htaccess redirect with 2 parameters

I am trying to get the following redirect done.
website.abc/index.php?page=foo // With one parameter
website.abc/foo
website.abc/index.php?page=foo&article=bar / With a second parameter
website.abc/foo/bar
And it has to be redirected backwards as well.
If i type website.abc/foo/bar it should internally being redirected to index.php?page=foo&article=bar
I can get it to work with one parameter, but as soon as i'm using the second, the URL looks like this and gives me the 404:
website.abc/foo&article=bar.
I have tried many different ways of making it possible, but nothing worked.
Does anyone know how to do this?
Go like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index\.php [NC]
RewriteCond %{QUERY_STRING} \bpage=([^&]+)\b [NC]
RewriteCond %{QUERY_STRING} \barticle=([^&]+)\b [NC]
RewriteRule .* /%1/%2/? [R=302,L]
RewriteCond %{REQUEST_URI} ^/index\.php [NC]
RewriteCond %{QUERY_STRING} \bpage=([^&]+)\b [NC]
RewriteRule .* /%1/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([^/]+)/?)?$ index.php?page=$1&article=$2 [L,NC]

Rewrite URL with ?-sign

In .htaccess i want to rewrite the URL so...
http://example.com/folder/file.php?folder/images/image0001.jpg
Becomes...
http://example.com/folder/images/image0001.jpg
I have been trying...
RewriteRule ^folder/file.php?(.*)$ http://example.com/$1 [R=301,L]
But ends up with a unwanted ?-sign...
http://example.com/?folder/images/image0001.jpg
How do i get rid og the ?-sign ?
You can't match against the ? in a rewrite rule, you need to match against either in a %{QUERY_STRING} or in %{THE_REQUEST}. If you want to rewrite it to http://example.com/folder/images/image0001.jpg, meaning that the file **is actually at /folder/images/image0001.jpg, then you want this:
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^folder/file\.php$ http://example.com/%1? [R=301,L]
If there isn't anything there, obviously that redirect will just result in a 404 Not Found. If this is supposed to be a way to make the URL look "pretty", then you need to rewrite the URL back to the query string:
RewriteCond %{THE_REQUEST} \ /+folder/file\.php\?([^&\ ]+)
RewriteRule ^folder/file\.php$ http://example.com/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^folder/(.*)$ /folder/file.php?folder/$1 [L]