As of now I have one generic query "view" which I'll use on most pages...
My rewrite is:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ $1.php?view=$2 [L]
My url is:
classroom.php?view=creation
I want it to look like:
classroom/creation
I have it working but it screwed up other urls
dashboard.php was just /dashboard
but broke when I did the first rewrite rule...
These things are a pain to understand.
My Question is how can i have /dashboard, /classroom, /classroom/creation work?
EDIT: I got it working with:
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.php [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ $1.php?view=$2 [L]
The first 2 rules are for external redirect, which turns .php into a directory like format rest of the rules are for the internal redirect which keeps the pretty URL and doesn't show the redirect.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect /classroom.php?view=creation to /classroom/creation
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+classroom\.php\?view=([^\s]+) [NC]
RewriteRule ^ /classroom/%1? [R=302,L]
# Redirect /classroom.php to /classroom and /dashboard.php to /dashboard
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(classroom|dashboard)\.php [NC]
RewriteRule ^ /%1 [R=302,L]
# Internally forward /classroom/creation to /classroom.php?view=creation
RewriteRule ^classroom/(.*)/?$ /classroom.php?view=$1 [L,QSA,NC]
# Internally forward /classroom to /classroom.php and /dashboard to /dashboard.php
RewriteRule ^(classroom|dashboard)/?$ /$1.php [L,QSA,NC]
Related
I'm looking to redirect example.com/subdir to example.com?page=subdir, while still keeping example.com/subdir in the URL. This way, I can dynamically "create pages" with different get requests. I've gotten down how to do this, but I can't seem to figure out how to prevent example.com/subdir/subdir from causing assets to load improperly. Here's my current code:
Options +FollowSymLinks
RewriteEngine on
# Redirect /page.php, /page.html, and /page.htm to /page
RewriteRule ^(.+)\.(php|html|htm)\/*$ /$1 [R,L]
# If the page is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
# Internally redirect /page to /index.php?page=page
RewriteRule ^(.*?)/?$ /index.php?page=$1 [NC,END]
# Redirect /index to /
Redirect /index /
Keep only these rules in your .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))\.(?:php|html)[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ index.php?page=$1 [L,QSA]
Make sure to use a new browser for your testing to avoid old cache.
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]
Hey guys I can't figure out why my redirect isn't working. Trying to redirect /home and /index to the main homepage... here's what's on my .htaccess. Is there something blocking or causing this?
Everything under the redirect 301 was there pre trial & error and wasn't sure if I need to change/remove anything.
I've been trying tons of different things and i'm only getting either a 500 internal servor page or /?page=home
redirect 301 /home http://example.com
Options -Indexes +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
RewriteRule ^stats\/$ awstats/awstats.pl?config=www.example.com [R,L]
RewriteRule ^awstats\/$ awstats/awstats.pl?config=www.example.com [R,L]
# Redirect any requests for html files to index
RewriteRule ^(.+)\.html index.php?page=$1 [L]
# Rewrite any request for subdirectories to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Please see my comment in the question, and try with the code below. Hopefully the code-comments and the new order of the rulesets will give you a clear indication of how this should work.
Options -Indexes +FollowSymLinks -Multiviews
RewriteEngine on
RewriteBase /
# Remove "www."
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=302,L]
# Remove ".html"
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*).html$ /$1 [R=302,L]
# Redirect "home" and "index" to document root
# > Here, the expression (home|index) means "home or index"
# > Trailing slash is optional
RewriteRule ^(home|index)/?$ / [R=302,L]
# Redirect "awstats" or "stats" to "awstats.pl"
# > The (aw)? makes "aw" optional
# > Trailing slash is optional
RewriteRule ^(aw)?stats/?$ awstats/awstats.pl?config=www.example.com [R=302,L]
# Rewrite any request for non-existing files to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
I'm having an issue where some of the redirects don't work without a / at the end of the URL. Additionally, sometimes I have conflicting redirects that rewrite the following redirect.
Global Rules
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
ErrorDocument 404 /404.php
Doesn't work without slashes
RewriteRule ^(.*)launch/$ /$1products/ [R=301,L]
RewriteRule ^(.*)solutions/$ /$1products/ [R=301,L]
Example:
When I search for /launch without the last / then I end up at my 404 page.
Conflicts for second and third
RewriteRule ^(.*)products/overview/heartbeat/$ /$1products/heartbeat/ [R=301,L]
RewriteRule ^(.*)heartbeat/$ /$1products/heartbeat/ [R=301,L]
RewriteRule ^(.*)heartbeat-beta/$ /$1products/heartbeat-beta/ [R=301,L]
Example:
When I search for /heartbeat-beta/ and expect to get /products/heartbeat-beta/ my result is:
/products/products/products/products/products/products/products/products/products/products/products/products/products/products/products/products/heartbeat-beta/
Your rewrite rules are not in correct order have some suspicious regex patterns. Have it in this order:
RewriteRule ^(heartbeat(?:-beta)?)/?$ /products/$1/ [R=301,L,NC]
RewriteRule ^products/overview/(heartbeat)/?$ /products/$1/ [R=301,L,NC]
PS: To make trailing slash optional use /?$ in your regex pattern.
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]