why does my rewrite rule put a question mark at the end? - .htaccess

to rewrite pages like:
mysite.com/eng/cat
to redirect to:
mysite.com/eng?sentence=cat
I made the following rewrite rule:
RewriteCond %{THE_REQUEST} ^GET\ /eng/([^&\s]+) [NC]
RewriteRule ^eng/ eng\?sentence=%1? [R=301,NC,L]
but when i request
mysite.com/eng/cat
it rewrites like this
mysite.com/eng?sentence=cat?
i dont know why it puts a question mark at the end, i also tried
RewriteCond %{THE_REQUEST} ^GET\ /eng/([^&\s]+) [NC]
RewriteRule ^eng/ eng\?sentence=%1 [R=301,NC,L]
and
RewriteCond %{THE_REQUEST} ^GET\ /eng/([^&\s]+) [NC]
RewriteRule ^eng/ eng\?sentence=%1\? [R=301,NC,L]
which dont make any difference. any help?
my htaccess file:
# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s/+eng/([^/?&\s]+) [NC]
RewriteRule ^ eng?sentence=%1 [R=302,NC,L,NE]
AddType application/x-httpd-php .htm .html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Options -Indexes
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

Just this rule should work:
RewriteCond %{THE_REQUEST} ^GET\s/+eng/([^/?&\s]+) [NC]
RewriteRule ^ eng?sentence=%1 [R=302,NC,L,NE]
Make sure to test in a different browser to avoid 301 caching pitfall.
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.
Suggested htaccess file:
# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php
AddType application/x-httpd-php .htm .html
Options -Indexes -MultiViews
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s/+eng/([^/?&\s]+) [NC]
RewriteRule ^ /eng?sentence=%1 [R=302,NC,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

Related

HTACCESS Rewrite to subdomain + rewrite url's

I have a question about my HTACCESS. I've been experimenting with my HTACCESS for a while, and finally got the url rewriting to work - in combination with my own written php router.
I want to rewrite the url's not only to make them appear cleaner but also because i use it for the api. So i can do a GET/POST/PUT/DELETE to api/users and add a new user, update or remove etc.
Now i use these lines for the URL rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*/api/(.*)$ api.php [QSA,L]
for the subdomain rerouting to a folder i use
RewriteCond %{HTTP_HOST} ^demo\.example\.com [NC]
RewriteRule (.*) http://example.com/demo/$1 [L,R=301]
The only problem now is that i can not seem to get these combined. I would like to have a url like:
demo.domain.com/api/user
If i use the two apart from each other they work like expected, but combined the url rewrite for the /api always fails. This is probably due to the rewrite condition of the subdomain, but i can't see how i can combine the two.
I found the solution, also i extended the htacces.. Here is my htaccess file:
# No Caching for page files
<filesMatch "\.(html|htm|js|css|php)$">
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
</filesMatch>
# Error pages
ErrorDocument 404 https://%{HTTP_HOST}/#/404
ErrorDocument 500 https://%{HTTP_HOST}/#/500
RewriteEngine On
# Rewrite www to non
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R,L]
# Rewrite http to https
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Ignore conditions for files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
# Rewrite /api to api.php
RewriteRule ^api/(.+)$ api/api.php [QSA,L]
# Rewrite /downloads to downloads.php
# RewriteRule ^download/(.+)$ download/download.php [QSA,L]

Friendly URL's on HTTPS (500 Internal error)

After enabling https on all site I'm getting (500 Internal Server Error ) when I browse anything with friendly url,
But the links without friendly url are Ok,
Here is my htaccess file:
#Options +FollowSymLinks
#Options -MultiViews
#Options All -Indexes
# This can be uncommented to compress js and css files
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
# This can be uncommented to set cache control for image, js, and css files
#Header unset ETag
#FileETag None
#<FilesMatch "(?i)^.*\.(ico|flv|swf|jpg|jpeg|png|gif|js|css)$">
#Header unset Last-Modified
#Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
#Header set Cache-Control "public, no-transform"
#</FilesMatch>
<FilesMatch "\.(htaccess|tpl)$">
Order Allow,Deny
Deny from all
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteRule ^([a-zA-Z]+)\.html$ $1.php [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.uae-shipping.net/$1 [R,L]
#Handle all category links. Static category text followed by ID, followed by path
#The "category" text here needs to be changed if the language variable category is changed
RewriteRule ^category\/(.+/)location/(.+)$ browse_categories.php?id=$1&location=$2 [L,NC,QSA]
RewriteRule ^category\/(.+)$ browse_categories.php?id=$1 [L,NC,QSA]
#Handle all location links. Static location text followed by ID, followed by path
#The "location" text here needs to be changed if the language variable location is changed
RewriteRule ^location\/(.+)$ browse_locations.php?id=$1 [L,NC,QSA]
#Rewrite pages
RewriteRule ^pages\/(.+)\.html$ page.php?id=$1 [L,NC]
#Rewrite Blog
RewriteRule ^blog.html$ blog.php [L,NC,QSA]
RewriteRule ^blog\/([^/]+)-([0-9]+).html$ blog_post.php?id=$2 [L,NC]
RewriteRule ^blog\/category\/(.+)-([0-9]+).html$ blog.php?category_id=$2 [L,NC,QSA]
#Rewrite listing/banner website out
RewriteRule ^out-([0-9]+)\.html$ out.php?listing_id=$1 [L]
RewriteRule ^out-([0-9]+)-([0-9]+)\.html$ out.php?listing_id=$1&banner_id=$2 [L]
#Rewrite listings
#Ignore any physical files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)\.html$ listing.php?id=$1 [L,NC]
RewriteRule ^([^/]+)/images\.html$ listing_images.php?id=$1 [L,NC,QSA]
RewriteRule ^([^/]+)/send-message\.html$ listing_email.php?id=$1 [L,NC]
RewriteRule ^([^/]+)/send-message-friend\.html$ listing_email_friend.php?id=$1 [L,NC]
RewriteRule ^([^/]+)/reviews\.html$ listing_reviews.php?id=$1 [L,NC,QSA]
RewriteRule ^([^/]+)/add-review\.html$ listing_reviews_add.php?id=$1 [L,NC]
RewriteRule ^([^/]+)/classifieds\.html$ listing_classifieds.php?id=$1 [L,NC,QSA]
RewriteRule ^([^/]+)/documents\.html$ listing_documents.php?id=$1 [L,NC,QSA]
RewriteRule ^([^/]+)/events\.html$ listing_events.php?id=$1 [L,NC,QSA]
RewriteRule ^([^/]+)/suggestion\.html$ listing_suggestion.php?id=$1 [L,NC]
RewriteRule ^([^/]+)/claim\.html$ listing_claim.php?id=$1 [L,NC]
#Rewrite classifieds
RewriteRule ^classified/[^/]+-([0-9]+)\.html$ classified.php?id=$1 [L,NC]
RewriteRule ^classified/[^/]+-([0-9]+)/images\.html$ classified_images.php?classified_id=$1 [L,NC]
#Rewrite events
RewriteRule ^event/[^/]+-([0-9]+)\.html$ event.php?id=$1 [L,NC,QSA]
#Rewrite sitemap
RewriteRule ^sitemap.xml$ xml.php?type=sitemap [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ 404.php?id=$1 [L,NC]
</IfModule>

Rewrite part of url from .htacces

i need to redirect my old url to new url structure
old is http://website.com/mp3/song-name/
new is http://website.com/song-name-mp3-download.html
How can I do this using mod_rewrite?
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^play/(\d+)/.*$ playlist.php?action=shareview&id=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ search.php?search=$1 [L]
RewriteRule 404.html 404.php
RewriteEngine on
RewriteCond $1 [A-Z]
RewriteRule ^/?(.*)$ /${lowercase:$1} [R=301,L]
<IfModule mod_deflate.c>
<FilesMatch "\.(php|js|css|mp3|wmv|flv|html|htm)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
ErrorDocument 404 /mp3/404.html
</IfModule>
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^mp3/([^/]+)/?$ $1-mp3-download.html [L,NC,NE,R=302]

Rewrite Rule /index.html -> /some-pretty-description

Basically I have:
www.myfancysiteicantcodemyself.com/index.html
with a rewrite rule so it becomes
www.myfancysiteicantcodemyself.com
But what I would really like is this:
*www.myfancysiteicantcodemyself.com/index.html
-> www.myfancysiteicantcodemyself.com/some-cool-words-for-indexpage
www.myfancysiteicantcodemyself.com/anotherpageA.html
-> www.myfancysiteicantcodemyself.com/some-cool-words-for-pageA*
etc.…
How do I achieve this please, without moving files and creating a 301 and google evaluating me from scratch?
PS:
atm my htaccess loks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(html?|php)\ HTTP/
RewriteRule ^index\.(html?|php)$ http://www.example.com/ [R=301,L]
ErrorDocument 404 /404.html
Header unset ETag
FileETag None
ExpiresActive On
ExpiresDefault "access plus 10 years"
AddHandler application/x-httpd-php .html .htm
You need to do a 301, since google will still try to go to /index.html and /anotherpageA.html, and you'll need to 301 redirect to tell search engines that the content has moved.
So, the easiest way to do this is actually rename your files and use a 301. Otherwise, you're going to have to both do a 301 redirect as well as an internal rewrite.
For example:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(html?|php)\ HTTP/
RewriteRule ^index\.(html?|php)$ http://www.example.com/some-cool-words-for-indexpage [R=301,L]
RewriteRule ^some-cool-words-for-indexpage$ /index.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /anotherpageA.html\ HTTP/
RewriteRule ^ http://www.example.com/some-cool-words-for-pageA [R=301,L]
RewriteRule ^some-cool-words-for-pageA$ /anotherpageA.html [L]
etc...

why does my rewrite rule without redirect give a 500 error?

i want to rewrite urls like
mysite.com/eng?sentence=cat
to show like:
mysite.com/eng/cat
I dont want a redirect just a rewrite. so I tried altering a rewrite rule i found, the example before i edited it had a R=301 in it originally:
RewriteCond %{THE_REQUEST} ^GET\ /eng\?sentence=([^&\s]+) [NC]
RewriteRule ^eng\?sentence=$ /english/%1? [R=302,NC,L]
but when i remove the R=301 from the 2nd line it gives a 500 error.
how can I write this kind of rewrite rule? Im not great with this regex stuff, nor am i familiar with this
my htaccess file:
# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php
AddType application/x-httpd-php .htm .html
Options -Indexes -MultiViews
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /eng\?sentence=([^&\s]+) [NC]
RewriteRule ^eng\?sentence=$ /english/%1? [R=302,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
Use line end anchor $ OR else just reply on RewriteCond
RewriteCond %{THE_REQUEST} ^GET\s/eng\?sentence=([^&\s]+) [NC]
RewriteRule ^ /eng/%1? [NC,L,R=302]
Most likely 500 is because of looping due to other rules in your .htaccess. I suggest you posting full .htaccess code in your question.

Resources