I have the following rewriting rules:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+Category\.php\?Category=([^\s&]+) [NC]
RewriteRule ^ /C/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+product\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)?$ product.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^C/([^/]+)/?$ Category.php?Category=$1 [L,QSA]
the redirect work fine if you work from the home page, however when click trough to a product page on the /C/Category Name page it cannot find the product page. It is searching like so
http://localhost/C/product.php?id=302-Product-name
I presume that is because it thinks you are in a different folder.
Not sure how to solve this.
I needed to add the <base href="/" /> so it will send all the links back to the root.
Thanks so much again #anubhava from previous questions
Related
I started to use friendly URL but I have two problems.
I have a link pointing to www.mywebsite.com/articles.php/321312/some-text-here
When I click on this link, on the next page all my links remains with a portion of the url.
For example, the clickable logo image points to www.mywebsite.com/articles/321312/index.php instead of www.mywebsite.com/index.php
Another problem is that articles.php only works with the .php extension.
My htaccess here
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^ / [R=301,L]
RedirectMatch ^/$ /articles/
RewriteRule ^([^/\.]+)/?$ $1.php [L,QSA]
RewriteRule ^articles/([a-z0-9-]+)/([0-9]+)/?$ /articles.php?id=$2&desc=$1 [NC]
</IfModule>
Thanks in advance.
Replace your .htaccess code with this:
RewriteEngine On
RewriteRule ^$ /articles/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/.]+)/?$ $1.php [L]
RewriteRule ^articles/([a-z0-9-]+)/([0-9]+)/?$ articles.php?id=$2&desc=$1 [L,QSA,NC]
You must also add this just below <head> tag of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
like a title i have a problem with this rewrite rule
I write this
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(font|css|js|images|remote) [NC]
RewriteCond %{REQUEST_URI} !^/(font|css|js|images|remote/.*)$ [NC]
RewriteRule ^index$ /page/index [QSA,L]
RewriteRule ^(.*)/(.*)$ /page/article&category=$1&post=$2 [QSA,L]
When i call ajax remote from /remote/check-function.php from pages he load full /page/post with variable sent by ajax.
How i can resolve this issue
Thanks in advance.
Best.
The RewriteCond only applies to the RewriteRule that comes immediately. Properly that the url /remote/check-function.php matches the last rule and is rewritten. You should add RewriteCond to check whether the request matches the existing files / directories before applying the last rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /page/article&category=$1&post=$2 [QSA,L]
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
Ok i've tried multiple versions of code on my .htaccess file and I still can't get it to work properly... I don't know if I'm putting it in the wrong folder or what is going on but, here is what I'm trying to accomplish.
This is my code
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([a-z0-9]+)/?$ index.php?zipcode=$1&location=$2 [L,QSA,NC]
Here is another version that I've tried
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?zipcode=([a-z0-9]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?zipcode=$1&location=$2 [QSA,L,NC]
Here is what I'm trying to accomplish... a URL that looks like this:
example.com/rentals/32746/florida
The "32746" and the "florida" come from a form on the index page of the site and then it gets passed over to the "rentals" folder through the URL.
The .htaccess code sorta works whereas it spits out a URL like this:
http://www.example.com/rentals/12345/arkansas?zipcode=12345&location=arkansas
But do you see the extra on the tail end? It's as if it's duplicating the results in the URL.
I currently have the .htaccess file in my "rentals" folder should it be in the root folder?
UPDATED VERSION
For those who were running into the same problem as myself... here is the actual code I ended up going with:
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?zipcode=$1&location=$2 [L,QSA,NC]
You need to add a ? to the end of the target path in your first Rule :
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?zipcode=([a-z0-9]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?zipcode=$1&location=$2 [QSA,L,NC]
As it descards the orignal query strings from the destination.
Clear your browser's cache before testing this.
I can not figure out how to exclude home page from rewriting.
In other words, .htaccess treats "example.com" as "example.com/user/index.php?name=" so it displays user specific content instead of my main page. I've tried bunch of things and nothing worked as I expected.
RewriteEngine on
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^about/([A-Za-z0-9-]+)/?$ /about/index.php?p=$1 [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteCond %{REQUEST_URI} !^/login/?$ [NC]
RewriteCond %{REQUEST_URI} !^/register/?$ [NC]
RewriteCond %{REQUEST_URI} !^/about/?$ [NC]
RewriteRule ^([A-Za-z0-9.]+)/?$ /user/index.php?name=$1 [L]
Hopefully you guys understand what my problem is.
Rules look fine, the + qualifier in your regex should make it so it won't match just the / request. You can maybe try adding this right below RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+(?:index\.php|)($|\ )
RewriteRule ^ - [L]