I am trying to redirect my page from /index.php to /index?action=login but my .htaccess file is not working. I checked with
a2enmod rewrite
it returned
Module rewrite already enabled
I wrote a simple expression to redirect from index.php to index. But this is even not working
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index/$ /index.php [R=301,L]
I can't understand the mistake I was doing thanks in advance
To redirect /index.php to /index.php?action=login you can use the following rule :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ /index.php?action=login [L,R=301]
The RewriteCondition RewriteCond %{QUERY_STRING} ^$ checks to see if there is no query string in the old uri . Otherwise without the condition you will get a redirect loop error since both the old and the destination uri are identical.
RewriteEngine On
RewriteRule ^([^/]+)/? index.php?action=$1 [L,QSA]
With this rule you redirect from /login to /index.php?action=login
Related
Recently, I tried using htaccess for my semantic cleaner urls. This is my code
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^product/([0-9]+)$ home.php?id=$1 [NC]
When I navigate to http://localhost/home.php?id=1 it returns the right thing but when I go to the http://localhost/products/1 it returns a 404 not found error.
I tried the question: htaccess rewrite returns 404 not found and htaccess problem 404 not found but nothing works
You can use the following :
RewriteEngine on
Options -Multiviews
#rewrite /filename to /filename.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ $1.php [L]
# /product/123 to home.php?id=123
RewriteRule ^product/([0-9]+)$ home.php?id=$1 [NC]
Your rule returns a 404 when the requested URI is /product/1 because your first rule rewrites it to an unknown location /product/1.php . I added a condition in the first rule so it checks if the request is actually for a .php file.
i'm using .htaccess to create clean urls.
here is a piece of .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^posts/page/([0-9]+)/$ posts.php?page=$1
RewriteRule ^posts posts.php
it works fine but problem is both urls work!
is there a way to only make site.com/posts/ work and return 'not found' on site.com/posts.php(or redirect to site.com/posts/)
think i read somewhere two urls for the same page is bad for seo.
You can create a rewrite rule that returns 404 HTTP code on every ".php" URL :
RewriteRule .*\.php$ - [R=404]
This is working fine for me. Hope it will work fine for you as well.
RewriteEngine On
Options -Multiviews
#For rewriting request to posts.php
RewriteCond %{REQUEST_URI} ^/posts/?$
RewriteRule .* /posts.php [L,END]
#For returning 404 on directly accessing /posts.php
RewriteCond %{REQUEST_URI} ^/posts.php$
RewriteRule .* - [R=404,L]
To redirect /posts.php to /posts you can use this
RewriteEngine on
RewriteCond %{THE_REQUEST} /posts\.php [NC]
RewriteRule ^ /posts [NE,L,R]
#Your other rules
If you want to redirect the orignal request to 404 , change the RewriteRule line to this
RewriteRule ^ - [R=404,L]
I'm trying to setup 301 redirections for a website that's build with Phalcon Framework.
The Problem
My redirection is working fine, but it's appending a ?_url= with the old url after the redirection.
Default .htaccess
Here's what comes with the Phalcon Framework in the .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
Example
When a user is going on http://example.net/fr/le-circuit/a-propos, it's redirecting to http://example.net/fr/circuit?_url=/fr/le-circuit/a-propos
As you can see, the ?_url= is the extra that I'm trying to remove.
My .htaccess
Here's my .htaccess without the redirection:
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirection for old website links
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ /fr/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ /en/ [L,R=301]
RewriteRule ^$ /fr/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
Attempt #1
I've tried the following Redirect directive before and after the default rewrite rules that came with Phalcon.
Redirect 301 /fr/le-circuit/a-propos /fr/circuit
Attempt #2
I thought my first attempt wasn't strict enough so I decided to use regex and RedirectMatch:
RedirectMatch 301 ^/fr/le-circuit/a-propos/?$ /fr/circuit
Just like before, I've tried it before and after the rules that came with Phalcon and it's appending the old url after the redirection.
Attempt #3
I've tried to hardcode the url in the redirection to see if the ?_url= would still append and ... yes it does.
RewriteMatch 301 ^/fr/le-circuit/a-propos/?$ http://example.net/fr/circuit
Does anyone know the problem or have an idea why it's appending the old url after the redirection?
Keep redirect rules before your other rules:
RewriteEngine On
# Redirection for old website links
RewriteRule ^fr/le-circuit/a-propos/?$ http://example.net/fr/circuit [L,NC,R=301]
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ /fr/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ /en/ [L,R=301]
RewriteRule ^$ /fr/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
Make sure to clear your browser cache while testing this change.
Make sure you clear your cache when working with 301 permanent redirects. Always debug them as 302 temporary redirects. Make sure your cache clearing prevents the redirection from happening when you comment out the redirects in your .htaccess file. Once you've done that, you can try the following suggestion:
Ditch the "_url" altogether by using REQUEST_URI from your bootstrap instead:
Use RewriteRule .* index.php [L] instead of: RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
Then use this line in your index.php bootstrap:
echo $application->handle(strtok($_SERVER["REQUEST_URI"],'?'))->getContent();
Rather than echo $application->handle()->getContent(); which defaults to handle $_GET['_url'].
I want to rewrite some url by using .htaccess rewrite
I have url something like this:
domain.com/index?/pid=10550
and want to rewrite users to
domain.com/index.php?pid=10550
So, what will happen is users will access that url in backend
I'm new to php and have read some blogs like this have no luck
Also, I have tried this
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^domain.com/index?/$ /domain.com/index.php? [L]
it says The page isn't redirecting properly
and some times shows me a directory
You can not match against querystring in RewriteRule's pattern. You will have to match against {THE_REQUEST} variable (a full request line sent by a client to the server) using a RewriteCond
RewriteEngine on
#Remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]
#redirect /index.php/?pid=123 to /index.php?pid=123
RewriteCond %{THE_REQUEST} /index\.php/\?pid=([^\s]+) [NC]
RewriteRule ^ /index?pid=%1 [L,R]
This will redirect /index.php/?pid=123 to /index.php?pid=123 .
I am trying to rewrite the following URL via .htaccess:
http://website.com/dealer_home.php?id=dealer1
The result I am aiming for is this, where dealer1 is the username which is set as variable:
http://website.com/dealer1
I have tried this rule in .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L]
However I get "Internal Server Error" message when trying to load any of the website pages.
Can you provide some advice where I am doing wrong?
EDIT:
I tried also RewriteRule ^(.*)$ dealer_home.php?id=$1 [PT] but no success.
Thank you!
Maybe it's a conflict with existing files/folders and root uri.
Try this code instead
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
This rule will match every url like domain.com/something if something is not an existing file or folder.
So if you have other rules then you should put them above this one.
EDIT: to avoid duplicate content and to redirect old format to new url format
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/dealer_home\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]