Use mod_rewrite to rewrite get arguments with slashes - apache

I have a website which has a few standalone webpages (like signup.php) and a main PHP file that displays the rest of the pages using a GET parameter.
I wish to rewrite HTTP requests to the server so that a request like:
http://example.com/website/settings/ would display the page
http://example.com/website/index.php?page=settings, without affecting all the other pages.
I'm quite new to mod_rewrite and all of my attempts have resulted in either a 404 or 500 response code. What is the correct way to write the RewriteCond and RewriteRule?

Add these rules in the htaccess file in the document root (where the website folder is in):
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+website/(?:index\.php|)\?page=([^&]+)/(&|\ |$)
RewriteRule ^ /website/%1/ [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^website/(.+)/$ /website/index.php?page=$1 [L,QSA]
Or you could instead put these rules in the website folder if that makes more sense:
RewriteEngine On
RewriteBase /website/
RewriteCond %{THE_REQUEST} \ /+website/(?:index\.php|)\?page=([^&]+)/(&|\ |$)
RewriteRule ^ %1/ [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ index.php?page=$1 [L,QSA]

Related

shorten url with htaccess

I have an old system and would like to shorten the URL.
At the moment:
www.site.com/app/views/accessories.php
I would like to open it like this:
www.site.com/accessories.php
I tried to do like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteRule ^(.*)$ app/Views/$1 [NC,QSA]
But this error appears "Internal Server Error". What is wrong?
My folder structure is as follows:
As you can see the files are not in the root. They are inside two app/views/ folders.
Because of that the pages are opening with big and ugly links.
www.site.com/app/views/acessorios.php
www.site.com/app/views/clientes.php
www.site.com/app/views/empresas.php
I wish I could open them like this:
www.site.com/acessorios.php OR www.site.com/acessorios
www.site.com/clientes.php OR www.site.com/clientes
www.site.com/empresas.php OR www.site.com/empresas
That is, without looking like app/views/ in the url.
1st solution: With your shown samples and attempts please try following .htaccess rules file. Make sure to place your .htaccess rules file along with your app folder(not inside it, along side it). Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /app/views/
##External redirect rules from here.
RewriteCond %{THE_REQUEST} \s/app/views/([^.]*\.php)\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite rules from here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]*\.php)/?$ /app/views/$1 [QSA,L]
2nd solution: Rules to redirect URLs to www.site.com/acessorios are as follows:
RewriteEngine ON
RewriteBase /app/views/
##External redirect rules from here.
RewriteCond %{THE_REQUEST} \s/app/views/([^.]*)\.php\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite rules from here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /app/views/$1.php [QSA,L]

how to change url to a clean url that works

I've been looking for an answer for my problem, however the things I tried didnt work out. What I've been trying to do is to create a beatiful url for this link:
mywebsite.com/blog_template?slug_url=blog-post-name
to
mywebsite.com/blog-post-name
To achieve this I tried the following code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog_template/([^/]+)$ /blog_template?slug_url=$1 [L]
But my code didnt work... Any advice?
Here’s the full htaccess my website:
RewriteEngine On
RewriteBase /
ErrorDocument 404 http://91.218.67.117/404/
ErrorDocument 500 http://91.218.67.117/500/
#redirect 404
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
#remove php
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
Considering you want to rewrite this request in backend to index.php(OR change it to appropriate file's name in case its some other php file). With your shown samples and attempts please try following .htaccess Rules.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{THE_REQUEST} \s/blog_template\?slug_url=(\S+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?slug_url=$1 [QSA,L]
ErrorDocument 500 /pages/errors/500.php
NOTE: Please keep your .htaccess file and index.php(OR any php file which is taking rewrite request in backend for that matter) in same path(your root etc).
Your attempt was close. Just a small fix:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ /blog_template?slug_url=$1 [L]
This relies on additional rewriting rules to get applied though, since /blog_template most likely is not a resource the http server can somehow "execute" immediately. So you may want to combine above rule with other rules, which you did not reveal to us, which is why I cannot say anything about that.
If you also want to redirect requests to the "old" URL, then that variant should do:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^slug_url=([^/]+)$
RewriteRule ^/?blog_template$ / [QSD,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ /blog_template?slug_url=$1 [L]
It is a good idea to start out using a R=302 _temporary_redirection and to only change that to a R=301 permanent redirection once everything works as expected. That prevents nasty caching issues on the client side.

URL Rewrite - Multiple query

I am trying to see how I can achieve the following rewrite rules.
From
https://localhost/site/?page=place&place=west
https://localhost/site/?page=location&location=cityname
To
https://localhost/site/place/west
https://localhost/site/location/city
I am able to change
https://localhost/site/?page=place to https://localhost/site/place but not with another additional query as mentioned above.
htaccess
RewriteEngine On
#Redirect /site/?page=foobar to /site/foobar
RewriteCond %{THE_REQUEST} /site/(?:index\.php)?\?page=(.+)\sHTTP [NC]
RewriteRule ^ /site/%1? [L,R]
# Internally rewrite new path to the original one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:site/)?(.+)/?$ /site/?page=$1 [L,QSA]
The above htaccess works for the following which is what I also need.
https://localhost/site/place
https://localhost/site/about
https://localhost/site/contact
With your shown attempts, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs. New rules are clubbed to your already existing rules.
RewriteEngine ON
RewriteBase /site/
##New rules from here......
RewriteCond %{THE_REQUEST} \s/site/?\?page=([^&]*)&place=([^&]*)\s [NC]
RewriteRule ^ /site/%1/%2? [R=301,L]
# Internally rewrite new path to the original one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^site/([^/]*)/(.*)/?$ index.php?page=$1&location=$2 [NC,QSA,L]

Rewrite URLs in .htaccess for replacing Query parameters with forward slash (id?=value)

I have made sure that rewrite engine is enabled and removing .php extensions is working so I know that isn't the issue.
what I'm trying to do is simply remove the ?id=value aspect of the URL, so basically making the URL look like such:
folder/medias/value
Instead of
folder/medias?id=value
My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^404/?$ /404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ 404.php [L,R]
With your shown samples/attempts, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for external rewrite.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rule for internal rewrite.
RewriteRule ^([^/]*)/([^/]*)/?$ $1?id=$3 [L]
You may try this code inside the /folder/.htaccess (create this file if it doesn't exist):
RewriteEngine On
# External redirect from /folder/media?id=val to /folder/media/val
RewriteCond %{THE_REQUEST} /(\S+?)\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /folder/%1/%2? [R=301,L,NE]
# Internal rewrite from /folder/media/val to /folder/media?id=val
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA]
Trailing ? in first rule is to remove query string from original URL.
%{REQUEST_FILENAME} !-f and %{REQUEST_FILENAME} !-d is to skip existing files and directories from rewrite in 2nd rule.

mod_rewrite redirect url if no file found to main page

I want to be able to Access my scripts without the .php, .html etc., so I already wrote
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
##same for other extensions
in my .htaccess file (note: this file lies not in the root-path), but I also want to Redirect every incorrect request to my main page, so that www.mysite.com/dir/incorrect will be rewritten to www.mysite.com/dir/.
But my first try (RewriteRule ^ / [R] after RewriteCond) redirected me to www.mysite.com/, my experiments with RewriteBase (RewriteBase . and RewriteBase /) didnt work and I also noticed that many similar scriptredirect to www.mysite.com/dir/index.php (www.mysite.com/dir/index in my case), but I really want to Redirect to www.mysite.com/dir/. Is there any way to achieve this?
Have it this way:
RewriteEngine on
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php [L]
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [L,R]