mod_rewrite GET form to clean URL - php

I have tried searching but only came up with this link which I can get to work, just not correctly to apply to my current situation.
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1? [R=301,L,QSA]
RewriteRule ^highscores/personal/(.*)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
RewriteRule ^highscores/personal/(.*) personal.php?user1=$1 [L,QSA]
RewriteRule ^highscores/skill/(.*) highscores.php?skill=$1 [L,QSA]
RewriteRule ^highscores/(.*) highscores.php?$1 [L,QSA]
As of right now, it will redirect
http://localhost/highscores/personal/?user1=test
to
http://localhost/highscores/personal/test
like it should.
But I have a compare function which submits a GET request like:
http://localhost/highscores/personal/?user1=test&user2=test2
which needs to come out like
http://localhost/highscores/personal/test/vs/test2
but it comes out like
http://localhost/highscores/personal/test&user2=test2
Edit: Resolved using help from this htaccess tester
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=([^&]+)&user2=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1/vs/%2? [R=301,L]
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1 [R=301,L]

You need to edit your rule
RewriteRule ^highscores/personal/(.*)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
to
RewriteRule ^highscores/personal/([^/]+)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
because of greedy regex. You need to break (.*) section if / is found

Related

.htaccess Rewrite rules causing empty $_GET

I am having a problem where on some of my clean url pages the $_GET values are returning as null.
My URL rewrites:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /$1 [L,R=301] !facebookexternalhit
RewriteCond %{HTTP_HOST} ^codesmite.com [NC] !facebookexternalhit
RewriteRule ^(.*)$ http://www.codesmite.com/$1 [L,R=301,NC,QSA]
# internally add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,QSA]
RewriteRule ^([0-9]+)/?$ index2.php?section=index&page=$1 [NC,QSA]
RewriteRule ^freebies/?$ index2.php?section=freebies&page=1 [L,NC,QSA]
RewriteRule ^articles/?$ index2.php?section=articles&page=1 [L,NC,QSA]
RewriteRule ^deals/?$ index2.php?section=deals&page=1 [L,QSA,NC]
RewriteRule ^freebies/([0-9]+)/?$ index2.php?section=freebies&page=$1 [NC,L,QSA]
RewriteRule ^articles/([0-9]+)/?$ index2.php?section=articles&page=$1 [NC,L,QSA]
RewriteRule ^deals/([0-9]+)/?$ index2.php?section=deals&page=$1 [NC,L,QSA]
RewriteRule ^article/(.*)$ article.php?stitle=$1 [NC,L,QSA]
RewriteRule ^deal/(.*)$ deal.php?stitle=$1 [NC,L,QSA]
RewriteRule ^freebie/(.*)$ freebie.php?stitle=$1 [NC,L,QSA]
RewriteRule ^legal/?$ legal.php [NC,L,QSA]
The pages I am having problems with are:
www.example.com/articles/
www.example.com/freebies/
www.example.com/deals/
I am trying to set a value on these pages based on $_GET['section'] but $_GET array is null.
There are also folders called "articles", "freebies" and "deals" located in the root directory (I do not know if this makes any difference to my problems).
What am I doing wrong?
Nevermind, as soon as I posted this question I noticed my mistake, forgot to change index2 to index from my test file to live file.
No problem with my code, silly mistake ^_^.

url should rewrite if page and query string is match

I want to redirect page if my url and query string is matched i have try much but no success, can you please told me where i done mistake.
My url is
http://xxx.xxx.xxx.xx/search/videos?search_query=xxx+xxx
and I want to redirect (rewrite using .htaccess) it to
http://xxx.xxx.xxx.xx/search/videos/xxx+xxx
I have tryed
RewriteCond %{QUERY_STRING} ^search_query=([A-Za-z0-9]*)$
RewriteRule ^search/videos$ search/videos/$1 [R=301,L]
RewriteCond %{QUERY_STRING} "search_query=" [NC]
RewriteRule ^(.*)$ search/videos/$1? [L,QSA]
RewriteCond %{QUERY_STRING} ^search_query=$1 [NC]
RewriteRule ^(.*)$ search/videos/$1? [L,QSA]
What i am doing wrong?
You can use the following rule
RewriteEngine on
#terminate the 2nd rewrite iteration
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# redirect "/search/videos?search_query?foobar" to "/search/videos/foobar"
RewriteCond %{QUERY_STRING} ^search_query=(.+)$
RewriteRule ^search/videos$ /search/videos/%1? [R=301,L]
#internally map "/search/videos/foobar" to "/search/videos?search_query?foobar"
RewriteRule ^search/videos/([^/]+)/?$ /search/videos/?search_query=$1 [NC,L]

url redirection issue using .htaccess

Below are the things i need to convert using .htaccess
If url is sample.xxx.com means i need to convert it to www.xxx.com/domain/sample
I url is sample.xxx.com/category/34/electonics.html means i need to convert it to www.xxx.com/domain/sample/category/34/electronics.html
3.After converting those things again i need to build request to the necessary pages
I have the following rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/domain/%1/$1 [P,QSA]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)$ index.php?domain=$1 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/$ index.php?domain=$1 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/news/([a-zA-Z0-9_\-]+)/([0-9]+)_([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ news.php?domain=$1&category=$4&news=$3 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/category/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ category.php?domain=$1&category=$2 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/gallery/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ gallery.php?domain=$1&category=$2 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/video/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ video.php?domain=$1&category=$2 [Nc,L]
But it fails
If i change line 4 to
RewriteRule ^(.*)$ http://www.example.com/domain/%1/$1 [NC,QSA]
means it will work but the browser shows the reconstructed url.
Anyone spot my error please.
If you keep http:// in target URI then it will indeed be full redirect.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|ftp|mail)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^((?!domain/).*)$ domain/%1/$1 [L,NC]
RewriteRule ^domain/([\w-]+)/news/([\w-]+)/([0-9]+)_([0-9]+)/([\w-]+)\.html$ news.php?domain=$1&category=$4&news=$3 [NC,L]
RewriteRule ^domain/([\w-]+)/category/([0-9]+)/([\w-]+)\.html$ category.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/([\w-]+)/gallery/([0-9]+)/([\w-]+)\.html$ gallery.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/([\w-]+)/video/([0-9]+)/([\w-]+)\.html$ video.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/(.+)/?$ index.php?domain=$1 [NC,L]

How to delete a subdirectory from a URL?

So I'm trying to turn my website from this:
http://profe5.com/Profe5-Web/public_html/login.html
To this:
http://profe5.com/login
I've been struggling to do this, but whenever I run it I get 404 error!
This is my htaccess:
DirectoryIndex Profe5-Web
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^profe5\.com$ [NC]
RewriteRule ^(.*)$ http://profe5.com/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [R=301,L]
RewriteRule ^Profe5-Web/public_html/(.*)$ $1 [R=301,L]
It would be so awesome if you guys could help me!
Thanks so much!
This should be your complete .htaccess:
DirectoryIndex Profe5-Web
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^profe5\.com$ [NC]
RewriteRule ^(.*)$ http://profe5.com/$1 [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+Profe5-Web/public_html/([^.]+)\.html [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ Profe5-Web/public_html/$1.html [L,QSA]
you might consider instead doing this via a virtual host; check with your hosting company about setting it up
If you want /login to map to Profe5-Web/public_html/login.html - try this:
RewriteRule ^([^.]+)$ Profe5-Web/public_html/$1.html [R=301,L]
The RewriteRule you already have, RewriteRule ^([^\.]+)$ $1.html [R=301,L] will map /login to /login.html, which doesn't seem to do what you need it to.

Combine htaccess rewrites

I am using php CodeIgniter and have the following rewrite rule in my .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Since I want the users to use the site through SSL, I would like to add the following rewrite rule:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.link.com/$1 [R,L]
However, when I do that, every page in a subdirectory will have /index.php/actual_page - the index.php before that is not desired. I thought that might come from having RewriteRule ^(.*)$ index.php/$1 [L] in there, but without the RewriteRule ^(.*)$ https://www.link.com/$1 [R,L] it works fine.
So, how can I rewrite the url to https without breaking the first rewrite?
Make sure the https redirect happens before the rewrite to index.php. You want it to look something like this:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.link.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Categories