It is duplicate i know but i am stuck in here , i also tried this link URL rewriting with PHP but i need to ask someone, i am new and [actually couldn't get it] and i couldn't search more because of somethings. the problem is i get 404 ERROR ... so anyone help ?
# BEGIN
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ Folder/en/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ Folder/de/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^fa [NC]
RewriteRule ^$ Folder/nl/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(en|de|fa)/?$ index.php?lang=$1 [QSA,NC,L]
</IfModule>
# END
Suggested Solution
A better approach could be a single script at the root folder that handles all requests. This is an idea that can be better described with examples:
www.domain.com/ always showing in the browser's address bar but going internally to www.domain.com/lang_handler.php?lang=sv or
www.domain.com/page1/ always showing in the browser's address bar but going internally to www.domain.com/lang_handler.php?lang=sv&target_page1=page1
This can be achieved in .htaccess with mod_rewrite directives. Here is an example:
RewriteEngine On
RewriteBase /
# Set managed languages here, except default (en)
RewriteCond %{HTTP:Accept-Language} ^(sv|ne|no).*$ [NC]
# Replace the names of the script and the parameters in the next 2 lines
RewriteCond %{REQUEST_URI} !lang_handler\.php [NC]
RewriteRule ^([^/]+)?/?$ lang_handler.php?lang=%1&target_page1=$1 [L,QSA]
# If no match, set English
# Replace the names of the script and the parameters in the next 2 lines
RewriteCond %{REQUEST_URI} !lang_handler\.php [NC]
RewriteRule ^([^/]+)?/?$ lang_handler.php?lang=en&target_page1=$1 [L,QSA]
Related
I know that there are too many questions and answers about this so i have to apologize in advanced but nothing works for me.
I lunched my website in Godaddy/Linux Hosting and trying to apply a rewrite for my urls. What it looks like now is
https://www.mywebsite.com/tours/tour_id=15
and i would like it ot be
https://www.mywebsite.com/tour/15 or even better insted of the ID to GET the title from the sql (which if is not possible through htaccess i can do it with php) My Own Title https://www.mywebsite.com/tour/My Own Title
I am really new in htaccess, below is what is already written in the file
# Redirects to https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.com
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# Remove trailing slash in the end
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
I would be grateful for any help
Add to top of .htaccess this rewrite rule:
RewriteEngine On
RewriteRule ^tour\/\d+$ /tours/tour_id=$1 [L]
I have the following case:
https://example.com/index.php?id=4 fetches a page and its alias from a database and rewrites it correctly. I am using the following htaccess rules for this:
RewriteEngine On
RewriteBase /
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
The above works correctly.
I now have an addition to this with the language where I am adding a Get Parameter called lang.
So for example, https://example.com/index.php?id=4 results in https://example.com/about and the page shows correctly. To get the French version of it, I type https://example.com/about?lang=fr. This also works correctly and fetches the correct page details.
What I am trying to do is make the page accessible via https://example.com/fr/about in the above example.
To do this I have done some additions to my .htaccess as follows:
RewriteEngine On
RewriteBase /
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
# If just the language is specified (ie example.com/en)
RewriteCond %{REQUEST_URI} ^/..$ [NC]
RewriteRule ^(.*)$ $1/
# If no language subfolder, default to 'en'
RewriteCond %{REQUEST_URI} !^/../ [NC]
RewriteRule ^(.*)$ /en$1 [R=301]
# If no page specified, default to home.
RewriteCond %{REQUEST_URI} !^/../.+ [NC]
RewriteRule ^/(..) /$1/ [R=301]
# If no ln query, tack it on
RewriteCond %{QUERY_STRING} !lang= [NC]
RewriteRule ^/(..)/(.*) /$1/$2?lang=$1 [R=301]
But I am getting an infinite redirect loop and the URL shows as follows:
https://example.com/enindex.php?q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=en
It seems like I'm almost there, can somebody help on this?
Yes, you are almost there, try to change this section
From:
# If no language subfolder, default to 'en'
RewriteCond %{REQUEST_URI} !^/../ [NC]
RewriteRule ^(.*)$ /en$1 [R=301]
To:
# If no language subfolder, default to 'en'
RewriteCond %{REQUEST_URI} !^/../ [NC]
RewriteRule ^(.*)$ /en/$1 [R=301]
I want to remove the default index.php that auto comes with Codeigniter.
I've been able to remove that with this code
RewriteRule ^(.*)$ index.php/$1 [L]
example.com/index.php/blog can now be accessed by example.com/blog
I later wanted to prefix the URL with a www i.e example.com/blog should redirect to www.example.com/blog with these rewrite rules
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
After adding this code above to the end of my .htaccess file, it began to misbehave.
If I enter www.example.com/blog into a URL bar it works fine but if I enter example.com/blog it redirects to www.example.com/index.php/blog
What I'm I doing wrong?
I want example.com/blog to redirect to www.example.com/blog
Note: I am using the Codeigniter framework.
Added: This code is just on top of the previous ones I have up. Maybe this is it's problem please HELP!!!
RewriteCond $1 !^{index\.php|[assests/images/themes/fonts/style/scripts/js/install]|robot\.txt|favicon\.ico}
Try this:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ example.com/$1 [L,R=301]
After much tricks and following the post given by #Tpojka I was able to come up with this
RewriteEngine On
RewriteBase /
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# no www -> www
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
This guy was my problem
RewriteCond $1 !^{index\.php|[assests/images/themes/fonts/style/scripts/js/install]|robot\.txt|favicon\.ico}
I would need help on how to exclude folders and some files like robot.txt file like I tried in the malfunctioning line.
This is the complete .htaccess file which I use personally:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Please read the question carefully before marking as duplicate.
We all know, that using in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
we can redirect all traffic to index.php so we can create friendly urls and have one front controller.
Although the question is connected to mod_rewrite the problem is described for Laravel.
The following .htaccess comes by default with Laravel 4 and it works fine:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If we run url mydomain.com/something and have set that route for something properly, some controller will be launched. It works fine so far.
However in Laravel 4 we will be able to reach the same route using mydomain.com/index.php/something. Probably using Laravel url creating we will have no urls with index.php in url but there is some other problem.
For example if our competition would like to make us some harm, they can simple put in Internet single links for urls to mydomain.com/index.php/something, mydomain.com/index.php/something2 and so on and search engines will see duplicate urls.
Of course if we have our custom PHP application, we can do it in PHP without a problem checking simply $_SERVER['REQUEST_URI'] and make 301 redirection. We can of course do the same in Laravel but we have to write this code in PHP each time and probably some developers could say it is bad practice to do it in PHP.
Question is simple: how can I redirect in .htaccess all urls that contain index.php to to the same url without index.php?
Example urls that should be redirected:
mydomain.com/index.php/something should be redirected to mydomain.com/something (something could be anything - can contain any characters)
mydomain.com/index.php should be redirected to mydomain.com
mydomain.com/index.php?anything should be redirected to mydomain.com (anything can contain any characters)
mydomain.com/index.phpanything should be redirected to mydomain.com anything can contain any characters)
Insert these rules just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
After spending hours I write below code for me and its 100% working
Redirect index.php to non index.php
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^index.php/(.*)$ /$1 [R=301,L]
how can I redirect in .htaccess all urls that contain index.php to to
the same url without index.php?
Add this to your .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
For Nginx, here is the rules :
location / {
rewrite ^/(.*?)index\.php[^/] /$1? redirect;
rewrite ^/(.*?)index\.php(?:/(.*))?$ /$1$2? redirect;
}
This solved my problem to force https & remove index.php from the url in Kohan 2.3
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
RewriteRule ^(application|system) - [F,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!localhost$|127\.0\.0\.1$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
There are many issues related to Apache configuration with mod_rewrite and mod_alias on Stack Exchange but I couldn't find an answer to my issue, so here is another question! ;)
I migrated an old website to a new location.
Articles used to be accessible to an URL such as http://xxx/blog/index.php?post/YYYY/MM/DD/title, so there are many links of that form through the existing webpages.
Now, the URL should be http://xxx/post/YYYY/MM/DD/title, so just remove blog/index.php? from the final URL.
I wanted to use mod_alias and a Redirect clause, but I read here that the Redirect clause was interpreted after the RewriteRule clause, which is a problem in my case because I'm already using a RewriteRule condition.
Here is my current .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
I tried to modify it this way:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/index.php? / [R=301,L]
RewriteRule ^(.*)$ index.php?$1
It almost works except:
http://xxx/blog/index.php?post/YYYY/MM/DD/title is redirected/rewritten to http://xxx/blog/?post/YYYY/MM/DD/title (note the ?) so it doesn't work
it looks like this new line messes up a lot of other URLs that do not start with /blog/index.php? such as the backend URL...
Any help will be more than welcome!
Edit (2014-07-16):
If I use the following rule:
RewriteRule ^/?blog/index\.php(.*)$ /$1 [R=301,L]
then going to
http://xxx/blog/index.php?test
takes me to
http://xxx/?test
which is almost correct (the interrogation mark is still a problem)!
But when I try to match the interrogation mark by adding \?:
RewriteRule ^/?blog/index\.php\?(.*)$ /$1 [R=301,L]
then it just stops working... Going there:
http://xxx/blog/index.php?test
just leaves me there!
Why is that?
Try adding this rule to the beginning of your set of rules:
RewriteCond %{THE_REQUEST} \?post/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^&\ ]*)
RewriteRule ^index\.php$ /post/%1/%2/%3/%4? [L,R=301]
so that it looks like:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \?post/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^&\ ]*)
RewriteRule ^index\.php$ /post/%1/%2/%3/%4? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1