Changing url with .htaccess - apache

I have a site like "abc.com/1_en-Application.html . Now i want to change it to abc.com/application.html.
I have .htaccess like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([0-9]+)-([0-9]+)_(.*)-(.*).html$ index.php?id=$1&page=$2&lang=$3 [L]
RewriteRule ^([0-9]+)_(.*)-(.*).html$ index.php?id=$1&lang=$2 [L]
</IfModule>
Please help me how can i do this ?

Add the 3 lines below the existing rewrite rules:
RewriteCond %{REQUEST_URI} /application.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^application.html$ index.php?id=1&lang=en [L]
If you don't have a real file called application.html in the web root directory, this will rewrite the URL and internally direct it to index.php
If application.html is a real file, it will be shown instead. In that case, the 3 lines will still work but not necessary to include.

Related

.htaccess URL rewrite not working, tried all possible options

I was trying to rewrite a URL for making my site SEO friendly, but .htaccess rewrite not seems to work.
My URL is
www.tasteofkochi.com/dine-detail.php?a=150
I need this to look like
www.tasteofkochi.com/sometext/150
I did the simple formula but it's not reflecting, intact nothing happens at all. If I add some junk char in htaccess, site crashes, which means htaccess is working fine. Also I added a formula to remove .php extn, and that too works fine. Only issue is with this one. Can anyone please help me. I enable rewrite in httpd and allow all in directories, still not working.
Below is my .htacces
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^detail/([0-9]+)/?$ dine-detail.php?a=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?p=$1 [L,QSA]
We can create pretty urls with .htaccess and php by mainly two files one is .htaccess and another index.php
Example

.htaccess rewrite rule to make longer url work

I have a rewrite rule that allows me to be able to have a url like this..
http://example.com/randyt
and convert it to
http://example.com/?aff=randyt
It's working however, I also have other links that need to be used such as....
example.com/members/aff/go/randyt
example.com/members
example.com/members/signup
and so on. The rewrite rule I have ignores those urls and just goes to
example.com
Please help me add something to this so it will not ignore any url with the /members or /members/.... in it.
Here is the code I am using now...
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) ?aff=$1 [L,QSA]
Sorry about the comments section I am new here lol.
So here is the above code with the added code that someone suggested
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) ?aff=$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/members
RewriteRule . - [L]
When I try to use the extended url it still goes back to the base url showing only index.php from the root directory.
When someone uses a url like this....
example.com/members/aff/go/randyt
I need it to stay in the members directory using that affiliate code. The current program should take care of that.
Here is the .htaccess code in the members directory
<IfModule mod_rewrite.c>
RewriteEngine on
# RewriteBase /members
RewriteRule ^public public.php [L]
RewriteRule ^js.php js.php [L]
RewriteRule !\.(js|ico|gif|jpg|png|css|swf|csv|html)$ index.php
</IfModule>
<IfModule mod_php5.c>
# php_flag magic_quotes_gpc off
</IfModule>
That code was pre-written by the company I purchased the program from.
So to sum this up, I need 2 different things to happen...
If someone uses:
example.com/randyt (randyt can be any username)
it needs to stay on example.com showing that page (index.php file)
If someone uses:
example.com/members/aff/go/randyt (again randyt can be any username)
it needs to stay in the members directory and use the purchased script.
Try this rule in /members/.htaccess:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /members/
RewriteRule ^public/?$ public.php [L,NC]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
You can add a condition to your .htaccess
RewriteCond %{REQUEST_URI} ^/members
RewriteRule . - [L]
This condition will check if the Requested URI contains a string starting with /member then it will be passed unchanged to its destination.

.htaccess rewrite css/js/images to subfolder

I actually thought there should already be a thread about this but i wasn't able to find one.
let's say i have a website with this structure:
/index.php
/.htaccess
/template
/template/css
/template/css/foo.css
I'm redirecting everything to index.php with .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ index.php?0=$1 [QSA,L]
Now I've got a problem with the css. that i could fix by placing another .htaccess in /template with this
RewriteEngine off
Options -Indexes
And then access the css file with http://example.com/template/css/foo.css but actually i would prefere to have just http://example.com/css/foo.css
So i tried to put in /.htaccess the following code (that didn't worked). What did i do wrong?
RewriteEngine on
RewriteBase /
RewriteRule ^/css/(.*) www/css/$1 [L]
RewriteRule ^/js/(.*) www/js/$1 [L]
RewriteRule ^(.*)$ index.php?0=$1 [QSA,L]
note I don't have any .htaccess in /template!
You should remove the htaccess file in the css folder. You just need to exclude rewrites to the template directory. You can do it all in a single file:
RewriteEngine on
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/template/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /template/$1 [L]
RewriteCond ${REQUEST_URI} ^/template/
RewriteRule ^(.*)$ index.php?0=$1 [QSA,L]
The first rule handles requests for the css directory, and the second rule is the one you had that routes to index.php except if the request is for template.

How can I use mod_rewrite on current directory only?

Currently I have these rules in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*).css style.php?u=$1 [QSA]
RewriteRule ^(.*).xml rss.php?u=$1 [QSA]
</IfModule>
This will rewrite the following URLs:
http://domain.com/user.css
http://domain.com/user.xml
But when I'm trying to grab a file from a subdirectory: http://domain.com/css/style.css it gets rewritten as well.
My goal is rewrite only for current directory and avoid sub-directories, since all real CSS files on sub-directories will be rewritten.
How I can avoid this?
You need to make your pattern more restrictive: this ^(.*).css will match ANYTHING with .css in it while this pattern ^([^/]+)\.css$ will be restricted to something.css (styles\something.css will not match it).
<IfModule mod_rewrite.c>
RewriteEngine On
# do not do anything to real files or folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
RewriteRule ^([^/]+)\.css$ style.php?u=$1 [QSA,L]
RewriteRule ^([^/]+)\.xml$ rss.php?u=$1 [QSA,L]
</IfModule>
The easiest way is to tell mod_rewrite to avoid rewriting if the file is a real file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*).css style.php?u=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*).xml rss.php?u=$1 [L,QSA]
I've added a [L] tag (end) because once a rule is applied you certainly doesn't need the next rule to be checked.
Now if the files really exists but you really want to handle them with a php script if no 'css' subdirectory is present on the url... let's try that:
<Location "/css">
RewriteEngine off
</Location>

Remove ".html" from URL via .htaccess for a WordPress website

Background information:
I've searched stackoverflow for a specific solution and couldn't find one that fixed my situation. Thanks in advance for any help you can offer. Your knowledge is appreciated.
I've decided to accept a contract to "convert" (in the client's words) a Joomla site into a WordPress site. Everything is going along smoothly, except that the Joomla site links to .html files, both in its navigation and in the content of 100+ posts.
Instead of going through each post one-by-one and updating the links or running a SQL command to remove ".html" from URLs, I've decided to put the pressure on .htaccess, with which I am somewhat comfortable.
What I'm trying to do ↓
In WordPress, I have custom permalinks enabled, and it is as follows:
/%category%/%postname%
Here's an example of what one of the old URLs in the posts looks like:
http://the-site.com/category/the-webpage.html
I need the htaccess file to tell the webserver to remove the .html so the user, after visiting "http://the-site.com/the-webpage.html" is instead sent to:
http://the-site.com/category/the-webpage
I'm setting up the page stubs to follow the file name of the Joomla pages, so http://the-site.com/category/the-webpage will work.
My question:
Can you help me discover the solution to removing .html from the URL when someone visits the site, even if the HTML file doesn't exist on the server?
Here's how the .htaccess file looked before I made changes:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Here's the latest .htaccess file as of 5:35pm Eastern:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
The ↑latest .htaccess changes work. Thanks Tim!
This will work to force an external redirection to your new URLs, but this may not be ideal for your situation. I'm still trying to think if there's a way to keep the redirection internal and update the variable that WordPress uses to determine which page to serve up, but so far I haven't thought of anything that would work.
Entire .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You want to use a URL rewrite
RewriteEngine On
RewriteRule ^(.*)\.html$ $1
This should do it. It will rewrite a request to site.com/category/whatever.html to site.com/category/whatever. it shouldn't be dependent upon the requested file existing.
<Directory /var/www/category>
RewriteEngine on
RewriteRule (.*)\.html$ /category/$1
</Directory>
This is the format for apache2.conf or virtual host files. Not sure if you use the command in .htaccess. It's best to take care of it in the server conf, if you can, as that is only parsed once, on server startup, and htaccess is parsed on each request.