Rewrite index.php to root with variable - php

I am setting up my htaccess and everything is working the way I want it to:
Options +FollowSymLinks
RewriteEngine on
# redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?mydomain\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI}/ [R=301,L]
#rewrite pages
RewriteRule ^photo/([^/]*)/([^/]*)$ /photo.php?slug=$1 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video.php?slug=$1 [L]
RewriteRule ^quote/([^/]*)/([^/]*)$ /quote.php?slug=$1 [L]
RewriteRule ^post/([^/]*)/([^/]*)$ /post.php?slug=$1 [L]
RewriteRule ^author/([^/]*)/([^/]*)$ /author.php?display_name=$1 [L]
RewriteRule ^tag/([^/]*)/([^/]*)$ /tag.php?tag_name=$1 [L]
RewriteRule ^category/([^/]*)/([^/]*)$ /category.php?slug=$1 [L]
RewriteRule ^blog/$ /blog.php [L]
RewriteRule ^videos/$ /videos.php [L]
RewriteRule ^photos/$ /photos.php [L]
RewriteRule ^coming-soon/$ /coming-soon.php [L]
RewriteRule ^404/$ /404.php [L]
#add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]
#error redirect
ErrorDocument 404 /404
How would I go about setting it up so that index.php?page=2 redirects to mydomain.com/2/

You can use:
#error redirect
ErrorDocument 404 /404
Options +FollowSymLinks
RewriteEngine on
# redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?mydomain\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI}/ [R=301,L]
#add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]
#rewrite pages
RewriteRule ^photo/([^/]*)/([^/]*)$ /photo.php?slug=$1 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video.php?slug=$1 [L]
RewriteRule ^quote/([^/]*)/([^/]*)$ /quote.php?slug=$1 [L]
RewriteRule ^post/([^/]*)/([^/]*)$ /post.php?slug=$1 [L]
RewriteRule ^author/([^/]*)/([^/]*)$ /author.php?display_name=$1 [L]
RewriteRule ^tag/([^/]*)/([^/]*)$ /tag.php?tag_name=$1 [L]
RewriteRule ^category/([^/]*)/([^/]*)$ /category.php?slug=$1 [L]
RewriteRule ^blog/$ /blog.php [L]
RewriteRule ^videos/$ /videos.php [L]
RewriteRule ^photos/$ /photos.php [L]
RewriteRule ^coming-soon/$ /coming-soon.php [L]
RewriteRule ^404/$ /404.php [L]
RewriteRule ^(\d+)/?$ /index.php?page=$1 [L,QSA]
Always keep redirect rules before internal rules and keep catch-all type rules at the bottom.

Related

htaccess Dynamic Redirect that changes URL in clients browser

I am trying to redirect /search.php?s=xxx to /search/xxx and actually show /search/xxx in client's address bar.
I am using the following instruction in the htaccess root and it works but doesn't change the URL:
RewriteRule ^search/(.*)$ /search.php?s=$1
I have tried adding [R=301] at the end of the statement, so:
RewriteRule ^search/(.*)$ /search.php?s=$1 [R=301]
But this does the opposite, meaning it changes /search/xxx to /search.php?s=xxx
Here is the entire .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^monumentum$ /new_monumentum.php
RewriteRule ^monumentum/(.*)$ /new_monumentum.php?mid=$1
RewriteRule ^nuntium$ /new_articulo.php
RewriteRule ^nuntium/(.*)$ /new_articulo.php?aid=$1
RewriteRule ^liber$ /new_liber.php
RewriteRule ^liber/(.*)$ /new_liber.php?lid=$1
RewriteRule ^introductio$ /new_pagina.php
RewriteRule ^introductio/(.*)$ /new_pagina.php?pid=$1
RewriteRule ^persona$ /new_profile.php
RewriteRule ^persona/(.*)$ /new_profile.php?cid=$1
#RewriteRule ^search$ /new_search.php
#RewriteRule ^search/(.*)$ /new_search.php?sea=$1
RewriteRule ^exitio$ /new_exit.php
RewriteRule ^intro$ /new_enter.php
RewriteRule ^novus$ /new_account.php
# Error 404: Paginas no encontradas
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
To redirect /search.php?s=xxx to /search/xxx, you may use this rule:
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} /search\.php\?s=([^\s&]+) [NC]
RewriteRule ^ /search/%1? [R=301,L,NE]
RewriteRule ^search/(.+)$ search.php?s=$1 [L,NC,QSA]
# place your remaining rules below here
RewriteRule ^monumentum$ new_monumentum.php [L]
RewriteRule ^monumentum/(.*)$ new_monumentum.php?mid=$1 [L,QSA]
RewriteRule ^nuntium$ new_articulo.php [L]
RewriteRule ^nuntium/(.*)$ new_articulo.php?aid=$1 [L,QSA]
RewriteRule ^liber$ new_liber.php [L]
RewriteRule ^liber/(.*)$ new_liber.php?lid=$1 [L,QSA]
RewriteRule ^introductio$ new_pagina.php [L]
RewriteRule ^introductio/(.*)$ new_pagina.php?pid=$1 [L,QSA]
RewriteRule ^persona$ new_profile.php [L]
RewriteRule ^persona/(.*)$ new_profile.php?cid=$1 [L,QSA]
RewriteRule ^exitio$ new_exit.php [L]
RewriteRule ^intro$ new_enter.php [L]
RewriteRule ^novus$ new_account.php [L]
# Error 404: Paginas no encontradas
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html

php - error document combined with mod_rewrite

I'm trying to set ErrorDocument 404 page by using ErrorDocument 404 /404.php, but it does not work, because I have mod_rewrite enabled... Is there some chance to check, if the page exist before it falls to mod_rewrite? I'm posting my htaccess down bellow... It is caused by last rule, which redirects everything to product.php - RewriteRule ^(.+?)/?$ product.php?url=$1 [L,QSA]
DirectoryIndex index.php
RewriteEngine On
ErrorDocument 404 /404.php
RewriteRule ^(admin|subdom)($|/) - [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^coming-soon/?$ coming-soon.php [L]
RewriteRule ^tabulky-velikosti/?$ tabulky-velikosti.php [L]
RewriteRule ^o-bambusu/?$ o-bambusu.php [L]
RewriteRule ^vymena-zbozi/?$ vymena-zbozi.php [L]
RewriteRule ^doprava-a-platba/?$ doprava-a-platba.php [L]
RewriteRule ^obchodni-podminky/?$ obchodni-podminky.php [L]
RewriteRule ^ochrana-osobnich-udaju/?$ ochrana-osobnich-udaju.php [L]
RewriteRule ^o-nas/?$ onas.php [L]
RewriteRule ^kontakt/?$ kontakt.php [L]
RewriteRule ^faq/?$ faq.php [L]
RewriteRule ^kosik/?$ cart.php [L]
RewriteRule ^blog/?$ blog.php [L]
RewriteRule ^search/?$ search.php [L]
RewriteRule ^blog/tag/(.*)/? blog.php?tag=$1 [L,QSA]
RewriteRule ^blog/(.*)/? blogDetail.php?url=$1 [L,QSA]
RewriteRule ^search/(.*)/? search.php?s=$1 [L,QSA]
RewriteRule ^zaciname/?$ category.php [L]
RewriteRule ^nakupovat/?$ category.php [L]
RewriteRule ^kategorie/?$ category.php [L]
RewriteRule ^prozkoumat/?$ category.php [L]
RewriteRule ^kategorie/((?:pan|dam|det)ske)-pradlo/?$ category.php?gender=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ product.php?url=$1 [L,QSA]
Is there anything I can do with that?
There is another way in which you can do this. You set the 404 via a RewriteRule and then set the ErrorDocument via URL instead of the file:
RewriteRule ^404/?$ 404.php
ErrorDocument 404 https://www.example.com/404.php
Place it below your last rule and remove the [L] flag from your last rule.
This method shouldn't cause an issue with mod_rewrite. Make sure you clear your cache before testing this.
EDIT
You could just add a condition to the last rule to exclude the 404.php page?
RewriteCond %{REQUEST_URI} !^/404.php
That would stop the Rewrite from happening for that page, so your code would be:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/404.php
RewriteRule ^(.+?)/?$ product.php?url=$1 [L,QSA]

.htaccess redirection with multiple rules

Basically what I want to do is :
1) redirect any .html request to .php page.
2) if any 404 happens , then it should redirect to www.domain.com
3) domain.com should be redirected to www.domain.com
4) www.domain.com/index.html should redirect to www.domain.com
Here is my htaccess code
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^fr/(.)*$ / [R=301,NC,L] # Added line
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . http://www.domsdain.com/index.php [L]
</IfModule>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,NC,L]
With above code 4th rule (www.domain.com/index.html should redirect to www.domain.com) is not working
You can refactor to use these rules:
ErrorDocument 404 http://www.examle.com/
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+index\.(?:php|html) [NC]
RewriteRule ^ http://www.examle.com/ [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(.+)\.html$ $1.php [L,NC]
RewriteRule ^fr/(.)*$ / [R=301,NC,L]
Make sure to clear your browser cache or use a new browser for testing.

Underscores to hyphens dynamic url

I have the following existing .htaccess in place
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule home index.php [L]
RewriteRule ^([^/]*)/$ /index.php?TopicID=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?RollID=$1&FrameID=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RedirectMatch 301 /cache(.*) //$1
ErrorDocument 404 http://www.example.com/error.php
I have many dynamically generated URLs which are being rewritten to this
www.example.com/abc_def_ghi/ OR
www.example.com/abc_def_ghi/abc_def_ghi/
and i'd like to end up with this
www.example.com/abc-def-ghi/ OR
www.example.com/abc-def-ghi/abc-def-ghi/
I've tried adding this to the end of my .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !.(html|php)($|?) [NC]
RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) http://%{HTTP_HOST}/$1 [R=301,L]
but i'm getting a 500 error.
Can anyone help?
Rearrange your rules like this:
ErrorDocument 404 http://www.example.com/error.php
RewriteEngine on
RewriteRule ^cache(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# if there is only one underscore then repalce it by - and redirect
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=302]
# if there are more than one underscores then "repeatedly" repalce it by - and set env var
RewriteRule ^([^_]*)_([^_]*)_(.*) $1-$2-$3 [E=USCORE:1]
# if USCORE env var is set and there is no underscore left then redirect
RewriteCond %{ENV:REDIRECT_USCORE} =1
RewriteRule ^([^_]+)$ /$1 [L,R=302]
RewriteRule home index.php [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ /index.php?TopicID=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?RollID=$1&FrameID=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]

Redirect 301 - try to redirect links from old page asp to new page php

I try to redirect my old link who stand good on Google to my new php site. I try with Redirect 301 but no luck, try with and without site domain but no luck. All way lead me to starting page. I don't know is it a problem because old page was on .asp? http://www.dalmaturist.hr/
Options -Indexes
RewriteEngine on
RewriteRule ^hrv/ponuda/izleti.asp /hr/izleti [L,R=301]
RewriteRule ^hrv/kontakti/karta.asp /hr/pakostane [L,R=301]
RewriteRule ^eng/offer/excursions.asp /en/excursions [L,R=301]
RewriteRule ^hrv/o_nama/index.asp /hr/najam-plovila/ [L,R=301]
RewriteRule ^hrv/ponuda/rent.asp /hr/najam-plovila/ [L,R=301]
RewriteCond %{QUERY_STRING} ^mjesto=1$
RewriteRule ^itravel/hrv/rezultati.asp$ /hr/smjestaj/? [L,R=301]
RewriteCond %{QUERY_STRING} ^mjesto=1&stranica=6$
RewriteRule ^itravel/deu/rezultati.asp$ /de/unterkunft/? [L,R=301]
RewriteCond %{QUERY_STRING} ^mjesto=1&stranica=2$
RewriteRule ^itravel/eng/rezultati.asp$ /en/accommodation/? [L,R=301]
RewriteCond %{QUERY_STRING} ^sifra_apartmana=12&view_date=01/6/2014$
RewriteRule ^itravel/deu/detalji.asp$ /de/bootscharter/? [L,R=301]
RewriteCond %{QUERY_STRING} ^sifra_apartmana=84&view_date=01/6/2013$
RewriteRule ^itravel/eng/detalji.asp$ /en/accommodation/? [L,R=301]
RewriteRule ^hr$ /index.php?lang=hr
RewriteRule ^en$ /index.php?lang=en
RewriteRule ^de$ /index.php?lang=de
## Hrvatski ##
RewriteRule ^hr/dalmaturist$ /dalmaturist.php
RewriteRule ^hr/placanja$ /placanja.php
RewriteRule ^hr/uvjeti$ /uvjeti.php
RewriteRule ^hr/smjestaj/(.*)$ /smjestaj.php?page=$1
RewriteRule ^hr/smjestaj_sortiranje$ /smjestaj_sortiranje.php
RewriteRule ^hr/objekt/(.*)/(.*)$ /smjestaj-objekt.php?SmjestajID=$1&SmjestajSeo=$2
RewriteRule ^hr/najam-plovila/(.*)$ /rent-boat.php?page=$1
RewriteRule ^hr/rent-boat_sortiranje$ /rent-boat_sortiranje.php
RewriteRule ^hr/brod/(.*)/(.*)$ /rent-boat-objekt.php?PloviloID=$1&PloviloSeo=$2
RewriteRule ^hr/izleti$ /izleti.php
RewriteRule ^hr/suvenirnica$ /suvenirnica.php
RewriteRule ^hr/pakostane$ /pakostane.php
RewriteRule ^hr/okruzenje$ /okruzenje.php
RewriteRule ^hr/kontakt$ /kontakt.php
RewriteRule ^hr/bicikli-skuteri$ /bike-scooter.php
RewriteRule ^hr/rezervacija$ /rezervacija.php
RewriteRule ^hr/thank-you$ /thank-you.php
## Engleski ##
RewriteRule ^en/dalmaturist$ /dalmaturist.php
RewriteRule ^en/payment$ /placanja.php
RewriteRule ^en/terms$ /uvjeti.php
RewriteRule ^en/accommodation/(.*)$ /smjestaj.php?page=$1
RewriteRule ^en/smjestaj_sortiranje$ /smjestaj_sortiranje.php
RewriteRule ^en/object/(.*)/(.*)$ /smjestaj-objekt.php?SmjestajID=$1&SmjestajSeo=$2
RewriteRule ^en/rent-boat/(.*)$ /rent-boat.php?page=$1
RewriteRule ^en/rent-boat_sortiranje$ /rent-boat_sortiranje.php
RewriteRule ^en/boat/(.*)/(.*)$ /rent-boat-objekt.php?PloviloID=$1&PloviloSeo=$2
RewriteRule ^en/excursions$ /izleti.php
RewriteRule ^en/souvenir-shop$ /suvenirnica.php
RewriteRule ^en/pakostane$ /pakostane.php
RewriteRule ^en/surroundings$ /okruzenje.php
RewriteRule ^en/contact$ /kontakt.php
RewriteRule ^en/bike-scooter$ /bike-scooter.php
RewriteRule ^en/reservation$ /rezervacija.php
RewriteRule ^en/thank-you$ /thank-you.php
## Njemački ##
RewriteRule ^de/dalmaturist$ /dalmaturist.php
RewriteRule ^de/wie-bezahlen$ /placanja.php
RewriteRule ^de/allgemeine-geschaftsbedingungen$ /uvjeti.php
RewriteRule ^de/unterkunft/(.*)$ /smjestaj.php?page=$1
RewriteRule ^de/smjestaj_sortiranje$ /smjestaj_sortiranje.php
RewriteRule ^de/objekt/(.*)/(.*)$ /smjestaj-objekt.php?SmjestajID=$1&SmjestajSeo=$2
RewriteRule ^de/bootscharter/(.*)$ /rent-boat.php?page=$1
RewriteRule ^de/rent-boat_sortiranje$ /rent-boat_sortiranje.php
RewriteRule ^de/boot/(.*)/(.*)$ /rent-boat-objekt.php?PloviloID=$1&PloviloSeo=$2
RewriteRule ^de/ausfluge$ /izleti.php
RewriteRule ^de/geschenkboutique$ /suvenirnica.php
RewriteRule ^de/pakostane$ /pakostane.php
RewriteRule ^de/umgebung$ /okruzenje.php
RewriteRule ^de/kontakt$ /kontakt.php
RewriteRule ^de/fahrrad-motorroller$ /bike-scooter.php
RewriteRule ^de/reservierung$ /rezervacija.php
RewriteRule ^de/thank-you$ /thank-you.php
ErrorDocument 401 http://www.dalmaturist.hr/
ErrorDocument 403 http://www.dalmaturist.hr/
ErrorDocument 404 http://www.dalmaturist.hr/
You can't match query string in Redirect directive. Use mod_rewrite rule:
RewriteCond %{QUERY_STRING} ^mjesto=1$
RewriteRule ^itravel/hrv/rezultati\.asp$ http://www.dalmaturist.hr/hr/smjestaj/? [L,R=301]
RewriteRule ^hrv/o_nama/index\.asp$ /hr/rent-boat/ [L,R=301]
And make sure this is placed right at top before other rewrite rules

Categories