Mod_rewrite for url with? - php

I can't find my answer on the other questions here in StackOverflow..
mod_rewrites generator are failing so I don't know how to do that..
I have that urls: http://example.com/?index&category=football
football are dynamic generator by php, so there is many categories.
So I want my URL like this: http://example.com/football
How to do that with mod_rewrite?
Thank you so much!

RewriteEngine on
RewriteRule ^/([a-zA-Z0-9]+)$ /index.php?category=$1
#RewriteRule ^/([a-zA-Z0-9]+)/$ /index.php?category=$1

Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+\?index&category=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /?index&category=$1 [L,QSA]

This should makes URL http://example.com/football works as if http://example.com/?index&category=football was used:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^/?([^/$]*) /?index&category=$1 [L]
Lines 2 to 5 (from RewriteCond %{REQUEST_FILENAME} -s to RewriteRule ^.*$ - [NC,L]) are optional: they makes any existing file named as a category name (eg. football) be served directly (not using /?index&category=...).

Related

.htaccess to search events with a nice looking url

I'd like code for a .htaccess file that when this is entered:
domain.com/event/granniesteaparty a file in the folder 'event' (display.php) takes the 'granniesteaparty' bit as though domain.com/event/display.php?search=granniesteaparty had been entered.
If tried all sorts of things but this is where I am at the moment:
RewriteEngine on
RewriteRule ^event/([A-Za-z0-9]+)/([0-9]+)/?$ /event/display.php?search=$1 [NC,L]
RewriteCond %{QUERY_STRING} s=([^&]+)
RewriteRule ^event /event/1? [NC,R=301]
I hope that makes sense, could someone point me in the right direction please?
You have too many capture groups in your first rule. Give this a try
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} ^GET\ /+event/display\.php\?search=([^&\s]+)
RewriteRule ^ /event/%1? [R=301,L]
RewriteRule ^event/(\w+)/?$ /event/display.php?search=$1 [NC,L]
Put the following code .htaccess inside event directory:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} !\s/+event/display.php?search= [NC]
RewriteRule ^(.*)$ event/display.php?search=$1 [R=302,L,NE]
RewriteRule ^event/display.php?search=$1(.*)$ /$1 [L,NC]
Update
If you want only to redirect any wrong page to the display page but internally so the following code will do it :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) event/display.php [L,NC]
So when someone request /event/whateverwrong it will be at the url as it is but internally will map to display.php

Rewrite in index.php

I have a problem with rewriting hrefs in my website. First of all I managed to remove all .php extensions from na URLs, then I added rewriting all mydomain.com/img?id=X to mydomain.com/img/X, but I have been struggling with one more thing for some time and I would appreciate any help. My website consists of many pages and the link to page number X is mydomain.com/?page=X (e.g mydomain/?page=3), but I want to rewrite that URLs to mydomain.com/page/X
Here is my .htaccess:
RewriteEngine on
RewriteBase /
<something not important>
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC]
RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^img/([0-9]+)/?$ img.php?id=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Right below this rule:
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC]
RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]
try adding:
RewriteCond %{THE_REQUEST} \ /+\?page=([0-9]+)
RewriteRule ^ /page/%1? [L,R]
RewriteRule ^page/([0-9]+)/?$ /?page=$1 [L]

Redirect multiple slugs htaccess

My htaccess code is
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/newhome/hello-1/ [NC]
RewriteRule .* newhome/hello-2/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/newhome/hello-3/ [NC]
RewriteRule .* newhome/hello-4/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/newhome/hello-5/ [NC]
RewriteRule .* newhome/hello-6/ [R=301,L]
Here only first condition is working but the other conditions are not working
can any one pls help me out
thank you
One way to keep your rewrite logic clean is to use skip rules. The first rule says "skip the next three if the URI already maps to a file or a directory. If you've got no other rewrites to do in this case just replace the SKIP=3 by an END if using Apache 2.4+ and LAST otherwise.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [SKIP=3]
RewriteRule ^newhome/hello-1/ newhome/hello-2/ [R=301,L]
RewriteRule ^newhome/hello-3/ newhome/hello-4/ [R=301,L]
RewriteRule ^newhome/hello-5/ newhome/hello-6/ [R=301,L]
Except that this may not do what you are intending. Consider the request Newhome/hello-3/something. This will match rule 3 so the URI will be replaced by the target newhome/hello-4/ -- that it the something bit will be lost. If you want to keep this content then try:
RewriteRule ^newhome/hello-1(/?.*) newhome/hello-2$1 [R=301,L]
etc.

htaccess rewrite rules, or a PHP issue?

I am writing a small web app mainly used just by myself so I'm not interested in fancy frameworks and page templating etc.
I need to be able to rewrite these urls:
/?page=parks
/?page=park&id=1
into
/parks
/park/1
Now, I have got very close with the following rules:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]*)$ /?page=$1 [L]
RewriteRule ^([^/]+)/([^/]+)$ /?page=$1&id=$2 [L]
and this works for most of the pages I have, but if I do the URL /settings it breaks. If I echo $_GET['page'] I get "inc". I have no idea why. Is this a PHP issue, or are my rules wrong?
You don't need RewriteCond %{REQUEST_FILENAME}\.php -f condition for this.
You can use:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ /?page=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ /?page=$1&id=$2 [L,QSA]

Replace GET variables with slashes .htaccess

I am trying to use .htaccess to make my url's "prettier".
Changing domain.com/page/val1
To domain.com/page.php?var1=val1
If possible, I would like it to work with 2 variables as well:
Changing domain.com/page/val1/val2
To domain.com/page.php?var1=val1&var2=val2
This is my current .htaccess file that works to remove the .php extension:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I've found other topics with solutions similar to adding something like this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1/$2?var1=$3&var2=$4 [L,QSA,NC]
I've tried many ways to modify it and it never seems to work.
Any help would be appreciated. Thanks.
Try this code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?var1=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?var1=$2&var2=$3 [L,QSA]

Categories