mod rewrite is not working in my template - php

I have a htm/css template with what appears to be mod_rewrite rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} gallery\_remote2\.php
RewriteCond %{REQUEST_URI} !/main\.php$
RewriteRule . - [L]
RewriteCond %{THE_REQUEST} /d/([0-9]+)-([0-9]+)/([^/?]+)(\?.|\ .)
RewriteCond %{REQUEST_URI} !/main\.php$
RewriteRule . /main.php?g2_view=core.DownloadItem&g2_itemId=%1&g2_serialNumber=%2&g2_fileName=%3 [QSA,L]
RewriteCond %{THE_REQUEST} /v/([^?]+)(\?.|\ .)
RewriteCond %{REQUEST_URI} !/main\.php$
RewriteRule . /main.php?g2_path=%1 [QSA,L]
RewriteRule ^presentation/([0-9]+)$ /presentation.php?id=$1
ErrorDocument 404 /notavailable.php
</IfModule>
This part is mine: RewriteRule ^presentation/([0-9]+)$ /presentation.php?id=$1
the url I have now is www.domain.com/presentation.php?id=1
and I want www.domain.com/presentation/1/
Mine is not working, and was wondering if my rules are wrong or conflictinf with the ones already there.
EDIT:
Ok I got this to work: RewriteRule ^presentation/([0-9]+)$ /presentation.php?id=$1 [L]
so www.domain.com/presentation/2 I want a rule with either no slash or slash at the end... but we cant deal with that later..
Now the problem is that although the id is being retrieved, the website acts as if there is a presentation directory, therefore all the relative paths like including the header don't work. Actually, I think the css links are broken.

Actually if you do want URL in the browser to change then have define your rewrite rule like this:
RewriteRule ^presentation/([0-9]+)$ /presentation.php?id=$1 [R=301,L]
That simply means return HTTP status 301 to the browser with the new URL.
And that way your problem of browser not finding image, css, image etc using relative paths will also be taken care of.

Related

from query to path in subdirectory HTACCESS

I've an https domain with WordPress installed in the root directory.
In the subdirectory "/test/" i made a web application that works using GET; the path is something like that:
https://www.example.com/test/file.php?url=urlname&etc=etc
I need to transform it in:
https://www.example.com/test/urlname/?etc=etc
I also need a redirect from the first type url to the second one.
It's the first time that i have to edit an htaccess file, and after searching on the web i tried this code
RewriteRule ^/?test/([^/]+)/$ test/file.php?url=$1&%{QUERY_STRING} [L,QSA]
RewriteCond %{REQUEST_URI} ^/test/file\.php$
RewriteCond %{QUERY_STRING} ^url=(.*)$
RewriteRule ^/?test/file\.php$ /test/%1/?%{QUERY_STRING} [L,R=301]
but it obviously doesn't work.
Can someone help me?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /test/file\.php\?url=([^\s&]+)(?:&(\S*))?\s [NC]
RewriteRule ^ /test/%1?%2 [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^test/([^/]+)/?$ test/file.php?url=$1 [L,QSA,NC]

How to 301 redirect URL's with "index.php?" in a path?

I am trying to redirect old URLs that look like this:
mysite.com/index.php?dispatch=products.view&product_id=30176
to new URL's that are SEO friendly and look like - mysite.com/category/product
I am using CS-Cart 4.3.1 with SEO add-on enabled, and when I have imported all the products and categories the URLs were automatically changed.
This add-on has 301 redirect functionality in admin, which enables me to create 301 redirects manually. However, something is wrong and it doesn't work with redirects that have "index.php?" in a path to start with.
I have tried redirecting old URLs without "index.php?" in a path - like:
"dispatch=products.view&product_id=30176" to "mysite.com/category/product" and it works, page is being redirected. But this is not the correct URL that is indexed by Google and need redirecting.
I suspect the issue is somewhere in Rewrite Conditions or Rewrite Rules on htaccess file that were created by system upon activation of SEO add-on.
This is how it looks like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
Options -MultiViews
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} ^api/(.*)$ [or]
RewriteCond %{REQUEST_URI} .*/api/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*api/(.*)$ api.php?_d=$1 [L,QSA]
RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml)$ [or]
RewriteCond %{REQUEST_URI} store_closed.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/(.*)$ $2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
</IfModule>
If anybody can think of a reason why the redirects are not working when "index.php?" is included in a path, I would greatly appreciate your help.
The built-in "301 redirects" feature does not support non-seo friendly URLs as old URL parameter. It means that you cannot use "index.php?dispatch=products.view&product_id=30176", but you can use "old_website_category/old_website_product" one.
To realize what you want, please add 301 redirects to the .htaccess file. E.g.
RewriteCond %{QUERY_STRING} ^product_id=30176$
RewriteRule ^/?index\.php$ http://example.com/category/product? [L,R=301]

htaccess redirect if folder exists else redirect to another folder

I would like to redirect with htaccess to a certain folder 'setup' if it exists.
Else it must do the stock standard redirect as it was :
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://%{HTTP_HOST}/$1 [L]
RewriteBase /
RewriteRule ^$ /site [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /api/index.php [L]
Now my htaccess-Fu are not nearly where it supposed to be. I read the posts I could find on SO but none of them helped for my specific case.
Then I have a sub-question: Is it bad practice to have this permanently in your htaccess file? Seeing as it would only exist the first time you setup the site?
I often see people doing this via PHP, but doing it via htaccess does seem like a great alternative!
So what you're looking for is something like:
RewriteEngine on
# The part your looking for: If setup is present and you're not in it redirect there!
# If the setup directory exists in the document root...
RewriteCond %{DOCUMENT_ROOT}/setup -d
# ...and you're not in setup..
RewriteCond %{REQUEST_URI} !(setup) [NC]
# ...redirect to setup - with a 302 (temporary redirect) to make sure the browser won't cache the redirect.
RewriteRule ^(.*) /setup [L,redirect=302]
# the rest of your htaccess should go here... for instance:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

SEO friendly URLs being appended?

I've built the following htaccess file from googling around and looking at different examples.
My file now consists of the following rules:
## Enable Rewrite Engine
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
## Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
## Rewrite URL to exclude wwww.
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
## Actual URL re-writes
RewriteRule ^profile/(.*)/? profile.php?pid=$1 [QSA,L]
RewriteRule ^profile/(.*)/(.*)/? profile.php?pid=$1&view=$2 [QSA,L]
From what I understand, this will rewrite URL's such as http://www.domain.com to http://domain.com, disable listing of directory files (+FollowSymLinks), as well as append a forward slash to urls such as http://domain.com/test and change it to http://domain.com/test/ so there's only one representation of that URL in search engines. Awesome! But, my URL keeps appending the part of the url to be re-written when I click on a link in one of my HTML breadcrumbs...
Example: http:/domain.com/profile/1/shouts/pictures/all/all/all/all/all/ if I continuously click on the same link... Why is this happening?
This is issue is driving me absolutely insane!
I've tried reading mod rewrite cheatsheets and tutorials, but none of them seem to make any sense to me :(
Edit:
I should note that I'm echoing my a href code with (relative?) links. Is this the correct way of doing things?
All
Edit 2:
I just tried using absolute links too, but getting the same issue. My PHP now reads:
<a href="<?php echo $websiteUrl. "profile/". $p_id."/all" ?>">
Where $websiteUrl = http://domain.com/ Any ideas anyone?
Your code is almost right but some minor corrections are still needed. Pls try this code:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
## Rewrite URL to exclude wwww.
RewriteCond %{HTTP_HOST} ^www\.(domain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=302]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
## Add trailing slash to url
RewriteRule (?!^.*/$)^ %{REQUEST_URI}/ [L,R=302]
## Your URL re-writes
RewriteRule ^profile/([^/]+)/?$ /profile.php?pid=$1 [NC,L,QSA]
RewriteRule ^profile/([^/]+)/([^/]+)/?$ /profile.php?pid=$1&view=$2 [NC,L,QSA]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

.htaccess file does not recognize index.php

I have created a .htaccess file that allows for vanity URLs. However, I am no longer able to type www.website.com without typing in the index file. I.e. I have to type in www.website.com/index.php in order to see the homepage. This is what my .htaccess file looks like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.website.com/profile.php?u=$1 [NC]
Anyone know how to fix this? Thank you all!
The way you defined your rule increased the complexity.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
Above rule means if file name is a directory of file process as it is. after that dont process farther.
RewriteRule ^(.*)$ http://www.website.com/profile.php?u=$1 [NC]
This rule means map any request uri to profile.php?u=
Now when you request / that is www.website.com it checks the first rule and it fails to match. Then it check the second rule and maps it to profile.php?u=.
One way to fix it, would be check *if $_GET['u'] is empty or / in profile.php. If it is then load the index.php.
Another way is to find a proper regular expression for your user names once found use it here.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(USERNAME_REGEX)$ http://www.website.com/profile.php?u=$1 [NC,L]
The best way to handle this is using PHP,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?uri=$1 [L]
Now index.php will get every uri you pass. Now you can process the URI in index.php.
It may be due to your server set up.
Try DirectoryIndex index.php (see http://davidwalsh.name/directory-index-homepage-htaccess )
Edit (due to me not reading the question properly in the first place)
Have you tried it without the RewriteRule .* - [L] line?

Categories