Here is my .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~new/
</IfModule>
Directory structure of my website is this:
Now what I want to do is to be able to visit paths like following:
domain.com/~new/pages/page1.php
But when I try to visit above URL I get redirected to 404 page.
What am I missing? Are ReWriteRule also needed?
RewriteBase doesn't do what you think it does. In fact, by itself it does nothing. RewriteBase is only necessary if you have relative path substitutions in your RewriteRule. And if you aren't doing that (ie. you are using root-relative or absolute paths) then you don't actually need it.
Are ReWriteRule also needed?
Yes, RewriteRule is what actually does the URL rewriting. And you need to do some URL rewriting.
So, basically, you want to internally rewrite your URLs from:
example.com/~new/pages/page1.php (Your "virtual" URL)
to example.com/pages/page1.php (The real URL)
RewriteRule ^~new/(.*) /$1 [L]
The (.*) captures everything after /~new/ and $1 is a backreference to this captured subpattern.
Related
I'm trying to rewrite the url on a site I have on localhost (port 8000), I have already set this up for certain other directorys but I can't manage to do it for this one. I compared it to several other working .htaccess files but there wasn't any difference. (The module is activated.) The site is in a subdirectory called HTF and the file single.php works just fine. The htaccess file is placed in the same directory.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule videos/([0-9]+)/$ /HTF/single.php?id=$1 [L]
I would like to get an url like localhost:8000/HTF/video/1232434 for exemple.
Any help appreciated, tested several types of htacces files, here a working exemple for a other subdirectory called sorted:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^image-post/([0-9]+)/(.+)\.php$ /sorted/image-post.php? id=$1&title=$2 [L]
RewriteRule ^pages/([0-9]+)/(.+)\.php$ /sorted/page.php?page_number=$1&cat=$2 [L]
Your rule will match videos, not video as you were wanting. Also it will only match with a trailing slash.
This will work
RewriteEngine on
RewriteRule ^video/([0-9]+)/?$ single.php?id=$1
localhost:8000/HTF/video/1232434
will not match
videos/([0-9]+)/$
because of the trailing slash /. Try /? to make the slash optional or leave away. And videos.
^/HTF/video/([0-9]+)/?$ /HTF/single.php?id=$1 [L]
or
^/HTF/video/([0-9]+)$ /HTF/single.php?id=$1 [L]
This shall do the job. Sorry can not try here.
I'm currently trying to make SEO friendly url's for my website using this script:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^show/([0-9]+)/$ show.php?phto=$1
RewriteRule ^index/$ index.php
So i've tested some things, and i've came up with these problems:
When i visit my website : blalba.com/index/ my layout file wouldn't include/show up. (using an layout.inc.php header/footer system)
Also how can i make it that the user can visit it with index and index/
I'm not so good at this..
Grz
Change your rule with this code (to make trailing slash optional):
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^show/([0-9]+)/?$ show.php?phto=$1 [L,QSA,NC]
RewriteRule ^index/?$ index.php [L,QSA,NC]
Also make sure for including style, js, images etc always use absolute path i.e. it should start with / or http://.
I have to redirect all my asp page to new website developed in PHP.
I had my asp page as,
abc.asp?id=82
Which need to be redirected to
siteurl/index.php/abc
Can any one help me with this in HTAccess?
I have tried with,
Redirect 301 /abc.asp?id=82 siteurl/index.php/abc
rewriterule ^/abc.asp?id=82$ siteurl/index.php/abc[R=301,L]
But this is not working and giving me a 404 error.
You can't match against the query string in either a Redirect or RewriteRule. It's not very clear what the scope you're trying to accomplish here. Is the id=82 important? Does abc mean "anything that's just letters"? Is siteurl a directory or a domain name? If it's strictly what you've attempted, then this is strictly how it'll work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=82($|&)
RewriteRule ^/?abc.asp$ siteurl/index.php/abc? [L,R=301]
You're making 2 main mistakes:
Rewrite rule matches only URI part and doesn't match query string
Rewrite rule in .htaccess doesn't match leading slash.
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 /
RewriteCond %{QUERY_STRING} ^id=82(&|$) [NC]
RewriteRule ^(abc)\.asp$ http://domain.com/index.php/$1 [R=302,L,NC]
Once you verify it to be working replace 302 (Temporary Redirect) with 301 (Permanent Redirect)
Just use
RewriteRule ^abc.asp$ siteurl/index.php/abc [R=301,QSA,L]
That should do the job.. Using the QSA flag will forward any GET paramater too, if that's what you meant with the title.
I'm trying to convert a query string;
http://atwd/books/course?course_id=CC100&format=XML&submit=Submit
Into a segment URI;
http://atwd/books/course/CC100/XML
I'm working in CodeIgniter.
I was looking at a stackoverflow answer that said to check CodeIgniter's URL segment guide, but I don't think there's any information on how to convert a query string into a segment URI. There is, however a way to convert a segment URI into a query string, which is bringing up a load of results from Google too.
Following another stackoverflow answer, I tried this in my .htaccess file but nothing seemed to work
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^$ /course/%1/format/%2 [R,L]
In my entire .htaccess file I have this;
<IfModule mod_rewrite.c>
RewriteEngine on
#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]
#Source: https://stackoverflow.com/questions/3420204/htaccess-get-url-to-uri-segments
#Format Course function requests
RewriteBase /
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^$ /course/%1/format/%2 [R,L]
</IfModule>
This is in my root directory of Codeigniter screenshot
My code in the .htaccess file isn't working, I refresh the page and nothing happens. The code to hide the index.php is working though. Does anyone know why?
The notion of "converting URLs" from one thing to another is completely ambiguous, see the top part of this answer for an explanation of what happens to URLs when redirecting or rewriting: https://stackoverflow.com/a/11711948/851273
There's 2 things that happen, and I'm going to take a wild stab and guess that you want the 2nd thing, since you're complaining that refreshing the page doesn't do anything.
When you type http://atwd/books/course?course_id=CC100&format=XML&submit=Submit into your browser, this is the request URI that gets sent through mod_rewrite: /books/course. In your rule, you are matching against a blank URI: RewriteRule ^$ /course/%1/format/%2 [R,L]. That's the first reason your rule doesn't work. The second reason why it doesn't work is because above that, everything except images and index.php and robots.txt is being routed through index.php. So even if you were matching against the right URI, it gets routed before your rule even gets to do anything.
You need to correct the pattern in your rule to match the URI that you expect to redirect, and you need to place this rule before the routing rule that you have. So everything should look roughly like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^/?books/course$ /course/%1/format/%2 [R,L]
#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]
</IfModule>
You'll need to tweak the paths to make sure they match what you are actually looking for.
To both redirect the browser and internally rewrite back to your original URL, you need to do something different.
First, you need to make sure all of your links look like this: /course/CC100/format/XML. Change your CMS or static HTML so all the links show up that way.
Then, you need to change the rules around (all before your codeigniter routing rule) to be something liek this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# redirect browser to a URI without the query string
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /books/course/?\?course_id=([^&]+)&format=([^&]+)
RewriteRule ^/?books/course$ /course/%2/format/%3? [R,L]
# internally rewrite query string-less request back to one with query strings
RewriteRule ^/?course/([^/]+)/format/([^/]+)$ /books/course?course_id=$1&format=$2&submit=Submit [L]
#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]
</IfModule>
I'm not going to address the misunderstanding already addressed pretty well in the other answer and comments, and I can't speak for CodeIgniter specifically, but having given their URL routing docs a quick skim, it seems pretty similar to most web frameworks:
You probably just want to direct all traffic (that doesn't match physical files) to the frontend web controller (index.php) and handle the URL management in CodeIgniter's routing, not a htaccess file.
To do that, your htaccess could be as simple as:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [QSA,L]
</IfModule>
This, as I said, will redirect any traffic that doesn't match an physical file such as robots.txt or an image to your index.php.
Then, using the routing as described in the docs (http://ellislab.com/codeigniter/user-guide/general/routing.html) you can take in parameters and pass them to your controllers as you see fit, there is no need to 'convert' or 'map' anything, your URL's don't need to resolve to /?yada=yada internally, based on your routing rules CodeIgniter can work it out.
You'll need wildcard routes such as this from the docs:
$route['product/:id'] = "catalog/product_lookup";
A rough example of what yours might end up looking like would be something like:
$route['course/:id/format/:format'] = "course/something_or_other_action";
If I'm understanding you correctly, you might be over-thinking it. I have something similar in my own code.
I have a controller named Source. In that controller, I have the following method:
public function edit($source_id, $year)
{
# Code relevant to this method here
}
This produces: http://localhost/source/edit/12/2013, where 12 refers to $source_id and 2013 refers to $year. Each parameter that you add is automatically translated into its own URI segment. It required no .htaccess trickery or custom routes either.
I just redeveloped an existing site from the ground up. The old site used pure HTML while the new site will draw content from a database. Now I need to redirect the old pages to the new URL structure, but I’m a bit confused about how to write the .htaccess rules for a file-to-file redirect.
In all the examples I’ve found, the first part of the rule looks like an absolute directory path from the root, but they only contain the part of the URL that immediately follows the domain name.
For instance, I want to redirect
https://garrettcounty.us/archives/12262011news.html
to
https://garrettcounty.us/news/20111226/house-fire-on-christmas-day
From the examples I’ve seen (both on StackOverflow and abroad), I guess the rule would be
redirect 301 /archives/12262011news.html https://garrettcounty.us/news/20111226/house-fire-on-christmas-day
but the actual path to the original file on the server is
/home/username/public_html/archives/12262011news.html
Should I use the directory path or the path from the domain?
I would love to be able to use a rewrite rule. Unfortunately, the original developer didn’t use a consistent file naming scheme so I’m faced with things like
12262011news.html
Jan-19-2012-Headlines.html
State-Of-The-Union-25jan2012.html
In the new model, I'm directing everything through index.php with
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
so if anyone knows of an easier way to map all the old pages to the new URLs, I’d love to hear it. As it stands, it looks like I have to redirect 70+ pages one-by-one.
Well you old URL doesn't have news title so obviously mod_rewrite cannot create it. However to redirect
https://garrettcounty.us/archives/12262011news.html
to
https://garrettcounty.us/news/20111226/
you can use code like this in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} =on
RewriteRule ^archives/(\d+)([^.]*)\.html$ https://garrettcounty.us/$2/$1/ [R=301,L,NC]
Path used in Redirect directives: For mod_alias or mod_rewrite you must use path relative to DOCUMENT_ROOT not the full path on filesystem.