"Flat links" with Apache - apache

I'm attempting to convert URLs with GET variables from something like
http://domain.com/?username=john
to
http://domain.com/john
using following the articles here and here.
Using the example in the first article (with a slight modification) - RewriteRule .* index.php - I have gotten that to work. The first problem is, I still want to be able to access the other files in the same directory. So the closest I've gotten so far is RewriteRule index\.php/(.+) index.php?username=$1, which is still not ideal because I don't want the filename in the URL, but there's another problem. index.php is set as the DirectoryIndex, and I have no idea how to match that.
I'm still very much a beginner at configuring Apache, so any help is greatly appreciated.

Try something like that
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteCond %{QUERY_STRING} \buser=\w+
RewriteRule .* user.php [L]
put instead of user.php the name of your script.

Related

htaccess only one file in subfolder and keeping the url

I need to put files in a new folder like this:
domain.com/newsite/newpanel/events.php
There are other files in the server like:
domain.com/events.php,
domain.com/oldsite/events.php etc.
Requirement 1:
I cannot redirect everything (because old implementations exist) with generic rules,
so I only want to redirect specific urls.
domain.com/events should now skip the old files and go to domain.com/newsite/newpanel/events.php
Requirement 2:
I tried something like this
RewriteRule ^events /newsite/newpanel/events/$1 [P]
but the url on the url bar will change. Is it possible for it to display domain.com/events?
thank you all!
EDIT: Since OP has mentioned different URLs in comments section so adding solution as per that here.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/CProjects/events/?$ [NC]
RewriteRule ^(.*)$ CProjects/folderA/folderB/events [L]
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs after putting these Rules into your .htaccess file.
RewriteEngine ON
RewriteCond ^/events/?$ [NC]
RewriteRule ^(.*)$ newsite/newpanel/events.php [L]

htaccess rewrite in subdirectory

This may be very noob question, but I am relatively new to web development and have googled a lot but could not found anything like mine. I have a simple htaccess.
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^stores/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ stores/profile.php?sid=$1
this works just fine like I want it
e.g stores/profile.php?sid=12 is rewritten into
stores/12/store-seo-name
and in the stores sub directory,I have a page that displays details of a product listed by each store which takes two params (store_id & product_id)
I want to rewrite it like this
item_view.php?sid=12&p_id=35 to
item/12/35/product-seo-name
I tried a lot of methods but I could not get it to work, and it gives me 404 error when I add htaccess file in the stores sub directory.
Any help would be appreciated.
Try using this rule:
RewriteEngine On
RewriteRule ^item/([^/]*)/([^/]*)/product-seo-name$ /item_view.php?sid=$1&p_id=$2 [L]
Make sure you clear your cache before testing this.

.htaccess question - redirects after the domain name

Apologies if this is answered elsewhere. I had a search for this on here, but I'm quite confused so I'm not 100% what to search for in the first place.
I have a Wordpress site which is at exampledomain.com. I also own exampledomain.co.uk, and I have put in the .htaccess file the follow lines:
RewriteCond %{http_host} ^exampledomain.co.uk [nc]
RewriteRule ^(.*)$ http://exampledomain.com/$1 [r=301,nc]
These work fine in terms of changing exampledomain.co.uk to exampledomain.com, but the moment I add in something after the exampledomain.co.uk (i.e. exampledomain.co.uk/page1) the .htaccess file doesn't change it so it tries to load.
Is there something I can add to the .htaccess file which will sort this, so that, for example, if I were to type exampledomain.co.uk/page1 it would redirect to exampledomain.com/page1 ?
Thanks,
Charlie
P.S. Apologise for the weirdly parsed example links, but as a new user it won't let me include more than two hyperlinks.
Why not simply do
RedirectPermanent / http://exampledomain.com
in the co.uk's config instead? mod_rewrite is very handy, but for a simple domain redirector, it's major overkill.
comment followup:
I'd go for something like this:
RewriteCond %{HTTP_HOST} exampledomain.co.uk$ [NC]
RewriteRule (.*) http://exampledomain.com/$1 [R=301,L]

trying to get mod_rewrite to work

I have this problem where I switched CMS from the old one to the new one so I am trying to redirect urls properly.
Here are some example urls I wanted to convert:
#-1) http://www.mysite.com/?dispatch=search_data&features=hash_tag
#-2) http://www.mysite.com/index.php?dispatch=search_data&features=hash_tag
Basically both of them are identical except the first one doesn't have index.php in the url.
I got the following to code to partially work:
RewriteCond %{QUERY_STRING} dispatch=(.*)
RewriteRule ^$ http://www.mysite.com/? [L,R=301]
The above code works for #-1 (when there is no index.php) but when there is index.php it doesn't work.
Any help / guidance is much appreciated.
Thanks
____________UPDATE_______________
As per request, here is what the the target url should be:
http://www.mysite.com/
What do you mean it "works"?
You should probably post an example of what you want the new url to look like given the old url.
But in any case, part of your problem is that the RewriteRule line is specifically excluding index.php and any other page on your site.
What you wrote basically tranlates to this:
if the query string contains "dispatch=...":
redirect requests FOR THE HOME PAGE ONLY (^$) to http://www.mysite.com/
RewriteCond %{QUERY_STRING} dispatch=(.*)
RewriteRule ^(index.php)?$ http://www.mysite.com/? [L,R=301]
have you tried changing the second part to include the "index.php" part?
RewriteRule ^$ http://www.mysite.com/index.php? [L,R=301]

How do I make .htaccess do what I want? :) (appending query string to url)

Currently my .htaccess looks like this...
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
It currently changes any /xxx.php file into /xxx. This is great for SEO. However, I also want Mr. htaccess to convert certain URLs into a URL + query string. For instance when user goes to
/specific/somerandominfo
Then somerandominfo is passed to the specific.php file. I normally have no problem doing this using rewrites, but because of my fancy catchall rewrite, I can't figure out how to do it.
For example if I add
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]
to my .htaccess, then hitting up /specific/somerandominfo just serves me a big fat 500 Internal Service Error.
Any help from you apache gurus out there would be so, so cool.
Thanks!
p.s. anybody want to also throw in any other cool SEO tricks that they like? I'll bake you cookies.
You are getting 500 error because your rules are creating an infinite cycle. Check apache error log to see if it is true. So you should design your rules properly. Maybe like that:
RewriteRule ^([^/]*)$ $1.php [L]
RewriteRule ^(.*)/(.*)$ $1.php?var=$2 [L]
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]
This is mostly correct. I'd just add the B flag, like this:
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC,B]
This causes the capture group $1 to be properly escaped for use in query strings. Note that you can still use QSA to retain the query parameters used in the original request (in addition to somerandominfo).
Perhaps you'll want to post your actual RewriteRule.