Rewriting subfolders using nice url - .htaccess

When I go to http://www.example.com/youtube/detail/abvcde in my browser, I want to internally rewrite the url to http://www.example.com/youtube/detail.php?id=abvcde. I tried to do this with the following .htaccess, but it gives a 404 error.
Currently my .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

Put the following code in a htaccess file in server root directory:
RewriteEngine On
RewriteRule ^youtube/detail/(.+)$ youtube/detail.php?id=$1 [L]

You can put this code in your /youtube/.htaccess
Options -MultiViews
RewriteEngine On
RewriteBase /youtube/
RewriteRule ^detail/([^/]+)$ detail.php?id=$1 [L]

Related

Use htaccess to mask few urls

How can I use htaccess to redirect a few static pages and keep the same url?
Example :
Redirect www.mydomain.com/url-that-remains.php to www.mydomain.com/page-number-23
My current htaccess file looks like that:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This code should work:
RewriteEngine On
RewriteRule ^url-that-remains\.php$ /page-number-23 [L,NC]
Try like this
RewriteEngine On
RewriteRule /somestaticpageonyourwebsite.html /yournewpage.html [R=302]
EDIT:
I don't know why the above doesn't work. This should work.
RedirectPermanent /somestaticpageonyourwebsite.html /yournewpage.html

Trouble with .htaccess mod_rewrite

Please can somebody take a look at my .htaccess code to help identify what's wrong.
I am trying to amend all example.php, example.html and example.htm URL's to index.php?filename=example.html
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(index\.php) - [L]
RewriteRule ^([^/\.]+).(php|html|htm)$ index.php?filename=$1.html&%{QUERY_STRING} [L]
</IfModule>
When I enter a URL like example.html I'm taken to the default 404 and not index.php
Try:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?filename=$1 [L,QSA]
</IfModule>
I'm not using the apache .hatacces for a while,
But try this one:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !^.*/(css|img|js)($|/.*$) [NC]
RewriteRule ^(.*)$ /index.php?filename=$1&%{QUERY_STRING} [L]
Basically what you need is to be sure that the URL didn't have already index.php. You accomplish it with RewriteCond.
After you redirect all to index.php passing the filename as a parameter.
You can add more exception to the RewriteCond like on this example, all content with /css, /js, /img will not pass to this rule.
You can check this thread: Using .htaccess to reroute all requests through index.php EXCEPT a certain set of requests also

.htaccess file not redirecting url

I have this .htaccess file which simply reroute URL from primary link ex- http://www.mywebsitename.com/somepage.php?id=some_id to http://www.mywebsitename.com/somepage/some_id
Here is the code for that-
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.ashurocks.in$
RewriteRule ^(.*)$ http://www.ashurocks.in/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([a-zA-Z0-9]+)$ /user.php?u=$1 [QSA]
RewriteRule ^group/([a-zA-Z0-9]+)$ /group.php?id=$1 [QSA]
RewriteRule ^article/([a-zA-Z0-9]+)$ /article.php?id=$1 [QSA]
It was working properly and I was able to get the content. But now, It's not working. Whenever I visit the URL http://www.mywebsitename.com/somepage/some_id, it does not redirect me to http://www.mywebsitename.com/somepage.php?id=some_id .
Is there something wrong with this .htaccess file code?
OK I got it.
I simply used Options +FollowSymLinks -MultiViews before the line RewriteEngine on and it solved my problem

.htaccess redirect all sub-directories to file

I have been trying to find a method to redirect all directories except a few to a file with a variable
Example:
site.com/test -> index.php?a=test
I want all subdirectories to be forwarded except for the following
/images
/admin
/includes
I have tried the following withough success.
RewriteCond %{REQUEST_URI} !^/images(/|$)
RewriteCond %{REQUEST_URI} !^/admin(/|$)
RewriteCond %{REQUEST_URI} !^/includes(/|$)
RewriteRule ^([\w/]*)$ index.php/?a=$1 [L]
Is there a way to complete this using .htaccess
EDIT:
I found this which is closer to what im looking for but it needs to be updated to allow access to /images /admin /includes.
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?a=$1 [NC,L,R=301]
In .htaccess you can use this rule
RewriteEngine On
RewriteRule !\.(js|jpeg|ico|gif|jpg|png|css|xml|html|swf|zip|tar|pdf|flv|gz|wmv|avi|rar|mp3)$ index.php [L]
In your application you can get $_SERVER["REQUEST_URI"] and manipulate as you whish!
<?php
$route = explode('/', $_SERVER['REQUEST_URI']);
print_r($route);

htaccess, sub folder rewrite to index.php

how can I do this?
main link:
http://www.domain.com/?link=whatever/something/everythting
convert to:
http://www.domain.com/whatever/something/everythting
I tried with this:
RewriteEngine On
RewriteRule ^([^/]*)$ index.php?link=$1 [L]
But not working.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
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
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ /?link=$1 [L,QSA]
This should work for you:
RewriteEngine On
RewriteRule ^(.+)$ index.php?link=$1 [L]
Edit:
As #anubhava posted in his answer, you should check whether the requested file or directory exists or not in a RewriteCond.
This should work:
RewriteRule ^([^.]+)$ index.php?link=$1 [L]

Resources