13 Replies - 1493 Views - Last Post: 29 June 2016 - 10:24 AM

#1 razer56   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 18-August 13

Creating user friendly URL with directory

Posted 27 June 2016 - 08:36 PM

I have a sign up page where the user can sign up. After signing up, they'll be redirected to their profile. The redirection is: example.com/profiles/?id=[SOME ID] . This works, but after I changed the URL in .htaccess to be a SEO friendly URL, the user gets redirected to a 404 page error where their profile is not found. In reality their profile does exist when typing in example.com/profiles?id=[SOME ID], but with example.com/profiles/[SOME ID] it says the profile does not exist. Here's my .htaccess

 <IfModule mod_deflate.c>
Options -Multiviews
RewriteEngine on

RewriteCond %{THE_REQUEST} /profiles/?\?id=([^&\s]+) [NC]
RewriteRule ^ /profiles/%1? [L,R]
RewriteRule ^(?:profiles/)?([0-9]+)/?$ /profiles/?id=$1 [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
    SetOutputFilter DEFLATE
    <IfModule mod_setenvif.c>
        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html

        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip

        # MSIE masquerades as Netscape, but it is fine
        # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

        # Don't compress images
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    </IfModule>
    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>


The dynamic index file is example.com/profiles/, where the profiles folder has an index.php. index.php are eliminated by using .htaccess. How do I make it so that example.com/profiles/[SOME ID] returns the actual profile and not a 404 error?

Is This A Good Question/Topic? 0
  • +

Replies To: Creating user friendly URL with directory

#2 Slice   User is offline

  • sudo pacman -S moneyz


Reputation: 253
  • View blog
  • Posts: 762
  • Joined: 24-November 08

Re: Creating user friendly URL with directory

Posted 28 June 2016 - 08:59 AM

Do you have mod rewrite enabled in your web server setup?

If you are using apache this should work (on ubuntu at least):

a2enmod rewrite

service apache2 restart



Or if you have access to your apache httpd.conf file, uncomment the line for LoadModule mod_rewrite:

LoadModule rewrite_module          modules/mod_rewrite.so



Or if you have access to your vhost file, you may need to add `AllowOverride all`.

If you're using nginx then I imagine it might be something similar if you have a google around.
Was This Post Helpful? 0
  • +
  • -

#3 razer56   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 18-August 13

Re: Creating user friendly URL with directory

Posted 28 June 2016 - 01:12 PM

I am using GoDaddy as my host, also it is worth noting that profiles in example.com/profiles/ is a directory and not a file. The PHP script that handles the dynamic data is example.com/profiles/index.php. Also another point worth putting out is that with
Options +Multiviews on, going to a URL like example.com/profiles/ takes me to the CSS file and not the PHP file. So
Options +Multiviews must be turned off.

Also, with GoDaddy this is what they say with mod_rewrite: "Because enabling mod_rewrite is handled at a global level, you do not need to enable it in your httpd.conf file. You only need to add the desired code to the body of your .htaccess file." My .htaccess file is in the root directory.
Was This Post Helpful? 0
  • +
  • -

#4 razer56   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 18-August 13

Re: Creating user friendly URL with directory

Posted 28 June 2016 - 03:58 PM

I have a page where the user signs up and they're redirected to their profile. Their profile URL is: example.com/profiles/?username=sam, where sam can be any name. This works successfully, but i'm trying to make the URL cleaner. I want to make the URL look like this: example.com/profiles/sam Here's my .htaccess:

<IfModule mod_rewrite.c>
DirectoryIndex index.php
Options -MultiViews

RewriteEngine On

RewriteCond %{THE_REQUEST} /profiles/\?username=([^&\s]+) [NC]
RewriteRule ^ /profiles/%1? [L,R=302]

RewriteRule ^profiles/([\w-]+)/?$ /profiles/?username=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

    SetOutputFilter DEFLATE
    <IfModule mod_setenvif.c>
        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        
        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        
        # MSIE masquerades as Netscape, but it is fine
        # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
        
        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
        
        # Don't compress images
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    </IfModule>
    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>


profiles in example.com/profiles/ is a directory. Inside profiles is an index.php file, this is the PHP file that creates the dynamic page. My .htaccess file is located in the root folder. I am also using GoDaddy. GoDaddy states that: "Because enabling mod_rewrite is handled at a global level, you do not need to enable it in your httpd.conf file. You only need to add the desired code to the body of your .htaccess file. The .htaccess file containing the rewrite rules must be in the same directory as the target files."

With my .htaccess like that, it changes the URL from example.com/profiles/?username=sam to example.com/profiles/sam but the profile is not shown at all, instead a 404 error page is shown, meaning the page does not exist. The page should exist. Also, adding Options +Multiviews produces the same 404 error. Also with Options +Multiviews, some pages display the CSS instead of the PHP page.

Someone mentioned an interesting point to me, he said: "[R] means a client-side redirect, which means you're rewriting and then redirecting the client to the new url. you don't have a "physical" /profiles/johndoe.php" page on your server, so you end up producing a 404. the urls the client sees in their browser should ALWAYS be the friendly ones, and then your rewrites translate those friendly/public urls into the "ugly" internal ones. you rewrites are going the other direction." but how do I accomplish this? How do I show the client the user friendly URL, but internally I translate the friendly ones into the ugly ones?

How can I make it so that example.com/profiles/?username=sam is redirected to example.com/profiles/sam and make the profile page actually show up?
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2225
  • View blog
  • Posts: 6,749
  • Joined: 15-January 14

Re: Creating user friendly URL with directory

Posted 28 June 2016 - 04:04 PM

Quote

How can I make it so that example.com/profiles/?username=sam is redirected to example.com/profiles/sam

What's the point in redirecting that? The links on your page should be the SEO versions, not the querystring versions. You wouldn't write the first one on your page in a link tag, you would write the second one. If someone manually types in the first one to try it, who cares? Remove the condition and rule on lines 7 and 8, and add a new condition to check for the SEO URL and rewrite it (not redirect it) as the "normal" URL.
Was This Post Helpful? 0
  • +
  • -

#6 razer56   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 18-August 13

Re: Creating user friendly URL with directory

Posted 28 June 2016 - 04:11 PM

That makes a lot of sense, but how do I check for the SEO URL and then rewrite it?
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2225
  • View blog
  • Posts: 6,749
  • Joined: 15-January 14

Re: Creating user friendly URL with directory

Posted 28 June 2016 - 04:29 PM

That's what line 10 does. If that's not working, you need to find a way to check the Apache logs for your domain through GoDaddy, if they make that available.
Was This Post Helpful? 0
  • +
  • -

#8 razer56   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 18-August 13

Re: Creating user friendly URL with directory

Posted 28 June 2016 - 04:47 PM

So by including just line 10 and the removal of the extensions code, www.example.com/profiles/Sam should redirect to www.example.com/profiles/?username=Sam internally correct?
Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2225
  • View blog
  • Posts: 6,749
  • Joined: 15-January 14

Re: Creating user friendly URL with directory

Posted 28 June 2016 - 04:53 PM

Right, it's going to rewrite that URL so that when the user requests the first one, Apache will process it as the second one.

Although you might need to specify the PHP filename, there's not really any reason to leave it out.
Was This Post Helpful? 0
  • +
  • -

#10 razer56   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 18-August 13

Re: Creating user friendly URL with directory

Posted 28 June 2016 - 04:59 PM

Apache will process the friendly URL as the ugly one, but SEO wise, will Google index the friendly URL? Also, the only reason why I'm not including the index.php file is because the rewrite rule removes the index.php, so I figure there's no need and it makes the ugly URL less ugly
Was This Post Helpful? 0
  • +
  • -

#11 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2225
  • View blog
  • Posts: 6,749
  • Joined: 15-January 14

Re: Creating user friendly URL with directory

Posted 28 June 2016 - 05:25 PM

Google will see whatever URL you write in the HTML.

Quote

and it makes the ugly URL less ugly

The rewritten URL is nothing that anything will ever see, it doesn't matter how it looks. The reason I suggest putting the filename there is because I don't know if Apache is going to apply things like the directory index rules to a rewritten URL. Maybe it's a 404 because it's looking for a file called "/profiles/?username=..." which does not exist. Explicitly telling it to load index.php will avoid that potential problem.
Was This Post Helpful? 0
  • +
  • -

#12 razer56   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 18-August 13

Re: Creating user friendly URL with directory

Posted 28 June 2016 - 08:28 PM

Weird for some reason including only line 10 doesn't redirect example.com/profiles/sam to example.com/profiles/?username=sam
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12510
  • View blog
  • Posts: 45,644
  • Joined: 27-December 08

Re: Creating user friendly URL with directory

Posted 28 June 2016 - 08:53 PM

Merged duplicate threads. Please do not duplicate post.
Was This Post Helpful? 0
  • +
  • -

#14 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2225
  • View blog
  • Posts: 6,749
  • Joined: 15-January 14

Re: Creating user friendly URL with directory

Posted 29 June 2016 - 10:24 AM

Yes, it will not redirect the browser. It happens internally with Apache, the user doesn't see it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1