WordPress like .htacces nice urls [duplicate] - .htaccess

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.htacces to create friendly URLs. Help needed
I want to build a script like WordPress for the "nice urls", i copied the .htaccess rows and made some modifications, but on the side of the PHP i don't know how to build it.
I don't understand how it works.

With the help of Daniel A White i found the class in WordPress files.
WP_Rewrite: http://xref.wordpress.org/trunk/WordPress/Rewrite/WP_Rewrite.html

Start with setting rewrite engine and follow sym links
RewriteEngine On
# Drop the .php -- or any other extension you use.
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#Forces the site to switch to www.site.com - change the site/TDL below
RewriteCond %{HTTP_HOST} ^insuranceiwant\.com$
RewriteRule (.*) http://www.insuranceiwant.com/$1 [R=301,L]
Now name your pages something useful like site.com/contact-us
If you are pulling dynamic pages from a database, also add this:
Options +FollowSymLinks
Options +Indexes
RewriteRule ^([A-Za-z_]+)_([A-Z]+)/([A-Za-z0-9_-]+)$ /Resources/city.php?state=$1&region=$2&city=$3
That example shows that the dynamic page has a state name, state abbreviation, and a city pulling from the database and showing the page like so:
insuranceiwant.com/California_CA/Los_Angeles
For more information on regular expresion for your page names check out this useful site:
http://www.regular-expressions.info/

Related

Htaccess for redirecting a directory to a file with the same name

After doing a quick lookup on how to manage sites with multiple language support, I find site.com/language/page/ the neatest url layout. (as I don't have the funds for site.language)
I have used htaccess to redirect the base site from site.com to site.com/language/
by using: RedirectMatch ^/$ /language/ where 'language' is language.html
But since I have a directory called /language/ so that all other pages in the given language can be put inside it, the site just shows up as the index of the directory.
How can I accomplish that kind of layout, if I want the main index page to show up in the url as site.com/language/ ?
Current htaccess that worked fine until I added the directory:
## Rewrite Defaults
RewriteEngine On
## Remove file extension + force trailing slash
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule (.*)/$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]
## Redirect to /en-gb
RedirectMatch ^/$ /en-gb/
With the root folder looking like:
/language
stuff.html
morestuff.html
language.html
...
I would really appreciate help as this is currently a nightmare situation. When I try compiling the htaccess with answers to similar questions, everyone has just parts of what I am looking to achieve and thus I break the layout with every modification...
Is it possible to change the back-end filenames and accomplish this layout using only htaccess for front-end url rewriting since the url bar is the only thing that matters?
If I understand correctly, you should be able to solve this by removing the line:
RewriteCond %{REQUEST_FILENAME} !-d
Let me know if that works.

SEO Friendly URLs & .htaccess

I'm trying to make my URLs SEO friendly but am having issues with my .htaccess
This is how my URL currently looks:
http://www.mysite.com/dns/?domain=stackoverflow.com&submit=Report
But I want it to show like this:
http://www.mysite.com/dns/stackoverflow.com
I've tried so many things I don't know if it's even possible to do so I just wanted to start over with guidance from you guys. The .htaccess I'm working with is in the /dns/ folder. Any help would be greatly appreciated.
Step 1: change all of your links and anchors in all of your pages to look like this: http://www.mysite.com/dns/stackoverflow.com, so when someone clicks a link on your site, they go to a link that looks like http://www.mysite.com/dns/stackoverflow.com instead of http://www.mysite.com/dns/?domain=stackoverflow.com&submit=Report
Step 2: In the htaccess file in your document root, add these rules (above any rules that you may already have):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?dns/(.+)$ /dns/?domain=$1&submit=Report [L,QSA]
Step 3: In the event that a GET method form submission is generating the link, you can add these rules too:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /dns/\?domain=([^&\ ]+)(&submit=Report)?
RewriteRule ^/?dns/?$ /dns/%2? [L,R=301]
See post url rewritin using .HTACCESS
www.mysite.com/dns/?domain=stackoverflow.com&submit=Report
to
www.mysite.com/dns/stackoverflow.com/Report
Try this
Options +FollowSymLinks
RewriteEngine On
RewriteRule \.(js|css|jpe?g|png|gif)$ - [L]
RewriteRule "^dns/([ \w-]+)/([ \w-]+)/?$" dns/?domain=$1&submit=$2 [L,QSA]

Got stuck in a very basic .htaccess rewrite

<h1>' . $name. '</h1>
The above is the reference which points to my profile1.php file. This file is called index.php . It currently displays the urls as this:
http://www.domain.com/interact/profile1.php?id=36
I have tried implementing the .htaccess file to rewrite the url. I tried many combinations and most of them gave a 500 error and some did not rewrite the url.
This is the .htaccess file which I use, it does not make the url to change.
I want the url to look like http://www.domain.com/interact/profile/36
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile1.php/$1 [QSA,L]
</IfModule>
I know this is a very basic question but I seem to stuck in it and have read basic tutorials but am not able to implement it properly.
The files index.php ,profile1.php and .htaccess are in folder named interact.
Tell me any changes required in php or .htaccess files.
It currently displays the urls as this: http://www.domain.com/interact/profile1.php?id=36
...
I want the url to look like http://www.domain.com/interact/profile/36
Step 1:
Change your content to have links like this:
<h1>' . $name. '</h1>
This way, when you click on a link the URL that will appear in the URL address bar is will look like: http://www.domain.com/interact/profile/36
Step 2:
Then you need to use these rules to internally change it back:
RewriteEngine On
RewriteBase /interact/
RewriteRule ^profile/([0-9]*) profile1.php?id=$1 [L,QSA]
In order to point any external links, like google index bots to the new URLs, you'll need to add these as well:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /interact/profile1\.php\?id=([0-9]*)
RewriteRule ^ http://www.domain.com/interact/profile/%2 [L,R=301]
try this
RewriteRule ^interact/profile1.php/(.*)$ interact/profile1.php?id=$1 [L]
i don't know what make QSA modifiers does so i remove it. If you know what, use it)

how joomla htaccess identifies what is the get method for particular alias?

Im developing a website which supports SEF urls. I use PHP as serverside language. I know htaccess basic codes how works with it. But the problem is if I want to rewrite a php get link I have to put each both links on htaccess like this.
RewriteRule ^sign-in$ index.php?view=signin
RewriteRule ^register$ index.php?view=register
RewriteRule ^jobs$ index.php?id=2
Is there any possible way to automate urls with htaccess and url particular alias instead of adding Rewrite rules manually? something like joomla? I was trying to understand how joomla htaccess connects with particual alias. But I still couldn't understand how it works. I cant uderstand how joomla htaccess makes relationship with article aliases. Please help. Thanks in advance.
If there isn't any difference for how an id looks like compared to how a view looks like (in your example, register is a view and jobs is an id=2), then you have to do one or the other individually:
To "automate" the views you could try just doing this:
RewriteEngine On
# all id's here:
RewriteRule ^jobs$ index.php?id=2 [L]
RewriteRule ^something-else$ index.php?id=3 [L]
RewriteRule ^another$ index.php?id=4 [L]
# this will do all views
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?view=$1 [L]
EDIT: If you need to do these mappings via an external set of aliases, you need to take a look at the RewriteMap directive. You will need to have access to the server or vhost configs in order to setup the map, but your rules can stay in an htaccess file.
Say you have a text file called "joomla_maps.txt" that looks like:
jobs id=2
another id=3
sign-in view=sign-in
register view=register
etc...
You can use that mapping by setting it up in a RewriteMap (in vhost/server config)
RewriteMap joomla txt:/path/to/joomla_maps.txt
And later in your htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?${joomla:$1} [L]
Take a look through the RewriteMap docs to get some examples of other kinds of maps, including executing a script or using a dbm hash map.

Htaccess rule to make urls like this ?page=1 look like /page/1 in Codeigniter

This is how my urls currently look:
http://mysite.com/?page=1
How can I make this work?:
http://mysite.com/page/1
There is a post on StackOverflow that asks the same question. But the accepted solution isn't working for me. Because I am using Codeigniter and my page results in a 404 perhaps because since the url pattern of a CI site is:
domain/controller/method
The system is assuming that I am requesting a controller called "page" and a method called "1" both of which of course doesn't exist. Or maybye it's due to a conflict with the other code in my htaccess file (which I downloaded from the CI wiki, it gets rid of index.php and does a few security things). Here is my entire htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users. Additionally this will allow you to create a System.php controller, 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder. This snippet prevents user access to the application folder. Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [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]
#Pretty urls for pagination links
RewriteRule (.*) index.php?page=$1
</IfModule>
The non indented bit is the solution I got from that other SO question that isn't working for me.
Any solutions to this CI pagination issue?
UPDATE
Ok, read some of the docs and now I have this working:
http://mysite.com/home/index/2
What would be the htaccess rule to turn that into?:
http://mysite.com/page/2
You should make this configuration at /application/config/routes.php (and let the .htaccess just for hide the index.php as you are already doing).
$route['page/(:any)'] = 'home/index/$1';
Or better, like #zaherg remembered (ensures that only numbers could by matched):
$route['page/(:num)'] = 'home/index/$1';
This way all the requests to http://mysite.com/page/2 will be treated internally as http://mysite.com/home/index/2 and so forth.
I suggest you take a look at CodeIgniter User Guide - URI Routing and CodeIgniter User Guide - Tutorial − Introduction.
Good luck.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
from CodeIgniter docs
That will handle removing the index.php, but what happens after that depends how CodeIgniter's query string handling is set up: it can be configured to use a query string rather than a path. See the link for more details.

Resources