URL Shortener - .htaccess update - apache

A while ago I made a little URL shortener which basically just redirects the user to the 'long url'.
The script that does as such is -cleverly- named shorter.php.
So, my problem:
When I first made the shortener, I made an .htaccess file like this:
RewriteRule ^s/(.*)$ shorter.php?u=$1 [NC,L]
Which allowed me to rewrite: doma.in/s/VAR to doma.in/shorter.php?u=VAR, which in turn redirects to the page that is linked to the VAR.
Last week I realised I could also make my URL shortener like this: dome.in/VAR
This can also be done in .htaccess, as far as I know.
The problem comes for me when I still have to support the URL with 's/' in between 'doma.in' & 'VAR'.
Help appreciated, thanks :).

Make ^s/ optional by using:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:s/)?(.+)$ shorter.php?u=$1 [NC,L,QSA]
Added 2 RewriteCond to make sure you don't rewrite existing files and directories.

Related

htaccess mod_rewrite - rewrite folder and file to parameters

I have the follow folder structure for images:
http://www.localhost/memes/01/blaa.jpg
http://www.localhost/memes/02/blaa2.pg
etc.
Now I want to move the structure but the old one must still be available for PHP file.
So it should be rewritten like:
http://www.localhost/memes/01/blaa.jpg
to:
http://www.localhost/memes/?folder=01&pic=blaa.jpg
or to:
http://www.localhost/memes/?pic=blaa.jpg (ignoring the subfolder of memes)
yes it's www.localhost for what ever reason and I don't mind it so far :D
Could you please try following, written and tested with your shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^memes/([^/]*)/(.*jpg)$ memes/?folder=$1&pic=$2 [NC,L]

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.

Change root directory in .htaccess

I have a problem with changing the root directory in .htaccess.
My folder structure looks like this.
What I want to achieve is, when I visit this page:
/comparty/about/
The page I will see is this page:
/comparty/pages/about/
I have already tried to search on Google, but the code I found did not work, though I tried to change it:
RewriteEngine On
RewriteBase /comparty/
RewriteRule ^(.*)$ pages/$1 [L]
I don't want it to redirect, I want to keep the same URL. Also I've had a big problem with Apache caching the .htaccess file, so I haven't been able to test many things.
Thanks in advance.
EDIT
I found a way to rewrite the URL from /comparty/pages/about/ to /comparty/about/ - this is the code:
RewriteEngine On
RewriteBase /comparty/
RewriteRule ^about/(.*)$ pages/$1 [L]
This only works on the about page, though. What would I have to do, to make it dynamic and work with every page?
You need to use a dynmic pattern :
RewriteEngine On
RewriteBase /comparty/
#if the request is not for an existent dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is not for an existent file
RewriteCond %{REQUEST_FILENAME} !-f
#rewrite the request to "/pages/request"
RewriteRule ^(.*)$ pages/$1 [L]
RewriteConditions above are important to avoid rewriting your existent files and directories to the /pages subfolder. Without those conditionrt the Rule will rewrite all requests including the destination path /pages and this may result in rewrite loop error.

Redirect (almost) all requests to the top level url

I've looked at a lot of the other mod_rewrite questions here and tried most of them, but none seem to work for me. This is what I'd like to do.
Redirect all requests like http://abc.com/foobar to http://abc.com/
EXCEPT images and js, so requests like http://abc.com/images/foo/bar or http://abc.com/js/foo/bar
The URL bar should stay the same. So while http://abc.com/foobar loads http://abc.com/, the URL should read like the former
Ports should remain intact, so http://abc.com:8080/foobar should redirect to http://abc.com:8080
This is what I have in my .htaccess file
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(images/.*|js/.*)$
RewriteRule ^(.*)$ - [L]
The condition is working well, and images and js files are loading fine. I thought the last line would redirect everything else to just the base domain, but I'm still getting 404 errors when I test it out.
I don't want to use a rule like this
RewriteRule ^(.*)$ http://abc.com/ [L]
because the domain may be different in different deployments.
I think I just have a poor understanding of how this works, but I'm just missing something small. Can someone help me get this sorted out?
This is what I ended up using
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(/*|/index.html)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^\.]+$ index.html [L]
It's not perfect, because the last rule only looks for files without a dot (.) in them. So it will not apply to http://abc.com/images/image.jpg but will apply to http://abc.com/images.
I don't really understand why I have to do it this way, but it works as it does.