I'm doing a website (for portuguese speaking countries) that displays phrases one at a time.
I would like to have simpler non-dynamic addresses for the site to be SEO friendlier and easier to memorize to the user. So my problem is this, and thanks in advance for all the help possible:
nfrases.com/ -> completely random phrase (don't need changing this!)
nfrases.com/index.php?id_frase=2222 -> phrase with ID (wanted this to be nfrases.com/2222)
nfrases.com/tag.php?tag_nome=amor -> random phrase with tag_nome (want: nfrases.com/amor)
nfrases.com/tag.php?tag_nome=amor&id_frase=2222 -> specific phrase with tag=amor (want: nfrases.com/amor/2222)
I think this is a noob question about mod_rewrite but that's exactly what I am! :D Already made several tries with the few knowledge I have but either i got 404 pages either the parameters wouldn't be received by the $_GET['tag_nome'] or $_GET['id_frase'].
The actual htaccess I have is this but it isn't working:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.nfrases.com$
RewriteRule ^(.*)$ http://www.nfrases.com/$1 [R=301]
RewriteRule ^([0-9]+)(.*) index.php?id_frase=$1
RewriteRule ^(.*)/([0-9]+) tag.php?tag_nome=$1&id_frase=$2 [L,QSA]
Can you guys help me?
Thanks again for everything! I appreciate your help.
The following rewrite rules work for me on a RewriteRule tester:
#removes trailing slash
#/vida/222/ becomes /vida/222
#/222/ becomes /222
RewriteRule ^(.+)/$ /$1 [R=301]
#/222 silently rewrites to /index.php?id_frase=222
RewriteRule ^([0-9]+)$ index.php?id_frase=$1 [L]
#/vida silently rewrites to /index.php?tag_nome=vida
RewriteRule ^([a-zA-Z-]+)$ index.php?tag_nome=$1 [L]
#/vida/222 silently rewrites to /index.php?tag_nome-vida&id_frase=222
RewriteRule ^([^/]+)/([0-9]+)$ tag.php?tag_nome=$1&id_frase=$2 [L]
You may want something like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
In this case, nfrases.com/2222 will rewrited as index.php/2222. This easily allows base routing in your index.php file. For more advanced routing, it may be faster and easier to use a good framework. Take a look at FuelPHP (http:www.fuelphp.com) or CodeIgniter (http://www.codeigniter.com).
Related
I've been crawling forums for about 2 hours and still haven't found the solution to my problem so I am turning to you guys for help.
My URL looks like this
http://sevalinmutfagi.com/list.php?id=1?kategori=borekler
I want it to look like this
http://sevalinmutfagi.com/tarifler/borekler
I try so much rewrite example but my index file at a subfolder in /View/ so I can't rewrite my URLs
here my .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?sevalinmutfagi.com$ [NC]
RewriteCond %{REQUEST_URI} !^/View/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /View/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?sevalinmutfagi.com$ [NC]
RewriteRule ^(/)?$ View/index.php [L]
This code works well for redirect subfolder all pages but I try to add rewrite URL codes can't work. I hope that has been revealing. Thanks to every for help.
that's worked for me fine.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ /subfolder/$1 [NC,L]
The FIRST thing to learn about mod_rewrite is when NOT to use it. Since mod_alias handles your specific task very nicely, that’s what I’d use:
Redirect 301 / /subfolder/
Since that looks like it would be “loopy,” you could fall back on mod_rewrite:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^subfolder/
RewriteRule .? subfolder%{REQUEST_URI} [R=301,L]
There’s just too much wrong with your attempt to cover … and I’ve addressed exactly those issues many times in many threads. Therefore, you might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too
I'll be honest in saying I have very little experience with .htaccess as I've always wanted to stay away from it as best I can. However, I've recently wanted to tidy up my urls and I've found that it's possible through .htaccess and rewriting.
Basically, I want to rewrite a url like:
www.mysite.com/profile.php?id=48194
To something like:
www.mysite.com/profile/48194
Here's the code I have currently:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^profile/(.*)/$ profile.php?id=$1
The line I'm trying to use is on the very bottom, RewriteRule ^profile/(.*)/$ profile.php?id=$1. The rest is used to remove the page extensions from the urls. I've changed $1 to $2 thinking perhaps it was conflicting with the code above, but nothing changed.
I also removed all the code except for RewriteEngine on and the last line thinking maybe the codes were conflicting but, again, nothing changed or worked. The rest of the code does work, removing the extensions from urls that is, so I know the rewrite thing is on.
Could someone try to break down and explain what I did wrong and how all this works? As well as providing a working example of the thing I'm trying to accomplish?
Thanks in advance!
Change order of your rules and use MultiViews option. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
RewriteEngine on
RewriteBase /
RewriteRule ^profile/([^/]+)/?$ profile.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
I'm currently using phpDolphin as my website social network, phpDolphin is not SEO URL ready, I was researching on how to fix the .htaccess for friendly seo urls and I cannot seem to deal with it. Here's my current .htaccess
Also here's how urls appear
http://feisbu.me/index.php?a=profile&u=cubaton3
and I want to show: http://feisbu.me/cubaton3
Here's my .htaccess file
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Please help me, thanks.
A few obvious things:
Mod_rewrite does not change the links you output. Your server side script needs to do that.
Your conditions and rules are kind of a mess, and you clearly need to hardwire your a=profile parameter. Remember that you're matching on, and rewriting the query string unless you specifically are using hostname variables, but they should not be relevant to matching, and are not part of the rewriting process unless you're issuing 301's.
This is typically done with something like this:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l
RewriteRule ^/(.*)$ /index.php?a=profile&q=$1 [L,QSA]
I'm not familiar with phpDolphin or other routes that this might interfere with, so you will probably need other rules. I personally am not a fan of these "if nothing default route to users" type of setups. Something like "/user/name" would be a cleaner route that will not interfere with other routes, since you can exact match on the parameters.
This is the solution, hope I helped, my site is gofinder.org:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?a=profile&u=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]
Okay I'm trying to use Lando (landocms.com) and I'm trying to get the pretty urls option to work.
Basically by default Lando creates link like: domain.com/index.php/page. Supposedly, there is a way to remove the index.php so the links become: domain.com/page. I have created an .htaccess as directed, however it does not work.
Here is the .htaccess I am using:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I have tried alot of variations, /index.php/, index.php? and plenty more but none work. According to HostGator everything should be fine. Any thoughts? I think I'm going crazy haha.
Thanks!
Rewriting for a CMS is a two-tier approach. First, you need to set your .htaccess (I have put a safer one here for you):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ index.php [QSA,L]
Then, LandoCMS allows you to remove the index.php from the generated addresses, by means of turning on the appropriate setting in the administration panel. See this link for more information.
If the .htaccess content I've given you doesn't work, then simply use the one that the CMS has given you.
You want to remove the index.php part from any URL, but process the incoming, friendly URLs through index.php nevertheless
RewriteEngine On
# remove index.php and redirect client
RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteRule ^/?index.php/(.*) /$1 [R,L]
# process friendly URL
RewriteCond %{REQUEST_URI} !^/index.php/
RewriteRule .+ /index.php/$0 [E=SEO:1,L]
The environment setting E=SEO:1 prevents an endless loop.
Trying to get www.example.com/test.php?archives=testing to www.example.com/archives/testing
According to godaddy they have mod_rewrite enabled by default
Here is my .htaccess file:
rewriteengine on
rewritecond %{HTTP_HOST} ^example.com$
rewriterule ^example\/(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(\w+)$ test.php?archives=$1 [L]
However this is not working, when i go to www.example.com/archives/test I get a 404, suggestions
I just left this in a comment but i might as well put it here so its seen easier. I wrote an answer for someone thats helped others out over time, and in the end this isn't exactly an answer to what your asking however its more of a stepping stone in the direction. The original question was asked how to work with short url strings and make them work in a fashion like your looking for, but rather copy and paste that answer here. Ill let you go there and read over it.
Its not to go without saying you will need to alter the rule a little for your specific needs but it will in the end serve its purpose for getting you where you want to be.
PHP dynamic DB page rewrite URL
You need some rewrite conditions to specify when this rule will be used. Without them, you will keep running the same rewrite rule indefinitely, giving you an error. Try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(\w+)$ test.php?archives=$1 [L]
How about this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(.+)$ /test.php?archives=$1 [L]
</IfModule>
Without is being super clear what you are trying to get from your rewrites I suggest:
Options -MultiViews
ErrorDocument 404 default
RewriteBase /