I have Some issue in .htaccess file - .htaccess

I have Some issue in .htaccess file.I have hosted my index page to webserver. I don't know what I did wrong in the .htaccess file.After adding code in .htaccess file still my url is not friendly.It is showing http://rang.comli.com/servicecnt It should be http://rang.comli.com/servicecnt/service_id/5
<html>
<head>
<title>Second mod_rewrite example</title>
</head>
<body>
<p>
welcome index page. Index page is loaded
Done with php my admin
</p>
</body>
</html>
Here is my .htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /public_html/index.php to /dir/index
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /public_html/index to /dir/index.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteEngine On
RewriteCond %{QUERY_STRING} ^service_id=([0-9]*)$
RewriteRule ^servicecnt\.php$ /servicecnt.php/%1? [R=302,L]

You can use:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+servicecnt\.php\?service_id=([^\s&]+) [NC]
RewriteRule ^ servicecnt/service_id/%1? [R=302,L]
## hide .php extension
# To externally redirect /public_html/index.php to /dir/index
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteRule ^servicecnt/service_id/(\d+)/?$ servicecnt.php?service_id=$1 [QSA,L]
## To internally redirect /public_html/index to /dir/index.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

Your best option is not to redirect the old url, but to let php generate the pretty url in the first place. Every time an user clicks on your link, the server needs to redirect the user, which is very inefficient. If the user would click the new url instead, you only have to internally rewrite it. You can use the following rule to rewrite it back:
RewriteRule ^servicecnt/service_id/([^/]+)/?$ servicecnt.php?service_id=$1 [L]

Related

Dropzone.js does not work because of htaccess URL rewrite

My htaccess file is interfering with the implementation of Dropzone.js on my website. Here is the htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
#Force non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Options -Indexes
Basically I am forcing a non-www redirect and am hiding the .php extension from the URL. However, I did not know that that would interfere with Dropzone.js, which requires a file name in the action attribute of the form or in the url property of the Dropzone object.
How can I hide the .php extension without interfering with Dropzone.js?
Thanks to everyone! I took out the .php extension from the action attribute and like Enyo suggested that fixed it.

.htaccess 301 Redirect + URL Rewrite

I got a .htaccess that pretty much looks like this:
# disable directory browsing
Options All -Indexes
# start rewrites
RewriteEngine on
Redirect 301 /someoldpage.php http://example.com/fancyurl
# if not a file, and not a directory, reroute through index as normal page
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
What I want to accomplish is that the url is rewritten also when someone is trying to access an old 301 redirectred url, so when going to http://example.com//someoldpage.php you see the url http://example.com/fancyurl and NOT like now: http://example.com/index.php?page=fancyurl
Redirect is part of mod_alias, while the rewrite stuff is part of mod_rewrite. The two different modules both get applied to the same request and thus causing it to redirect and rewrite at the same time. What you want to do is have it redirect if the request is /someoldpage.pjp and not rewrite to index.php, so you need to use only mod_rewrite here:
# disable directory browsing
Options All -Indexes
# start rewrites
RewriteEngine on
RewriteRule ^someoldpage\.php$ http://example.com/fancyurl [L,R=301]
# if not a file, and not a directory, reroute through index as normal page
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
I Struck with this problem and got a solution in my case, to rewrite the url from example.com/about.php to example.com/about with the following code in your htaccess.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]

Redirection through .htaccess

I am developing a PHP website and I want to redirect php to html and I write the below code in htaccess file. Code is working fine and all the php pages are redirect to html but when I put file name with php in url site is also open with .
RewriteRule ^(.*)\.html$ $1.php [nc]
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[?\s] [NC]
RewriteRule ^ /%1.html [R=301,L]
RewriteRule ^(.+?)\.html$ /$1.php [L,NC]

.htaccess sexy url rewrites

I want to get all my domains to rewrite and look good. for example:
example.com/blog.php?article=the-name-of-article
to become
example.com/blog/the-name-of-article
so i want the .php?article= to be changed into a /
my current .htaccess file looks like:
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
but all that does is drop the .php
I have been searching around but I just want to hear how everyone accomplishes this so easily.
RewriteRule ^blog/(.*)$ blog.php?article=$1 [QSA,L]

htaccess with sub site

I have an htaccess file under the main site
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php or .html extension
# To externally redirect foo.php ot foo.html to foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.(php|html) [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
That works for
http://www.site.com/tenant-referencing/
but I can't get the subsite to work
http://www.site.com/newwebsite/tenant-referencing/
I have the same htaccess under the subsite
Thanks

Resources